0

I am a complete beginner with C++. I am trying to debug my code because I am sure there are some pointer errors and such that I am still trying to understand but I can't compile it. The code is originally from a java program I wrote which is just a song class with some compare methods and I have transcribed it to c++ to try and learn the differences between the languages, the code itself is showing no errors but it just can't compile without that error popping up. I have tried looking at solutions to this error but have found nothing works so here is my code for win32 console project. Thank you for the help.

     // ConsoleApplication4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

class Song  {
private:
    string Artist;
    string Title;
    string Lyrics;
public:
    static int Sortcount;
    static int Searchcount;

    Song(string A_Artist, string T_Title, string L_Lyrics) {

        Artist = A_Artist;
        Title = T_Title;
        Lyrics = L_Lyrics;
    }

    //Compares the artist of one song to another. 
    class ArtistComparator {

    public:
        int compare(Song *o1, Song *o2) {
            string Artist1 = (*o1).Artist;
            string Artist2 = (*o2).Artist;
            Searchcount++;

            if (Artist1.compare(Artist2) == 0){
                return 0;
            }
            Searchcount++;
            return (*o1).Artist.compare((*o2).Artist);
        }

    };

    //Compares the title of one song to another.
    class TitleComparator {

    public:

        int compare(Song arg0, Song arg1) {

            string Title1 = arg0.Title;
            string Title2 = arg1.Title;
            return Title1.compare(Title2);
        }
    };


public:

    //Testing method to make sure the Song class works and 
    //the compareTo method works.
    int main(int argc, char** argv){
        Song test1 = Song("cat", "bat", "this is not a real song");
        Song test2 = Song("cat", "apple", "also not a real song");
        int compareResult = test1.compareTo(test2);
        if (compareResult == -1){
            std::cout << "test1 comes first";
        }
        else{
            cout << "test2 comes first";
            cout << test2.toString();
        }

    };

    string getArtist(){
        return Artist;
    };

    string getTitle(){
        return Title;
    };

    string getLyrics(){
        return Lyrics;
    };

    string toString(){
        return Artist + ", " + Title + ", " + Lyrics;
    };

    //compareTo method used for sorting songs.
    //increments Sortcount each time it is called to keep track
    //of the efficiency of the sort algorithm.
private:
    int compareTo(Song other){
        Sortcount++;
        int art = Artist.compare(other.Artist);
        if (art == 0){

            return Title.compare(other.Title);
        }
        else
            return art;
    }
};
Tyler
  • 1
  • 1

1 Answers1

0
#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

class Song  {
private:
    string Artist;
    string Title;
    string Lyrics;
public:
    int Sortcount;
    static int Searchcount;

    Song(string A_Artist, string T_Title, string L_Lyrics) {

        Artist = A_Artist;
        Title = T_Title;
        Lyrics = L_Lyrics;
    }

    //Compares the artist of one song to another. 
    class ArtistComparator {

    public:
        int compare(Song *o1, Song *o2) {
            string Artist1 = (*o1).Artist;
            string Artist2 = (*o2).Artist;
            Searchcount++;

            if (Artist1.compare(Artist2) == 0){
                return 0;
            }
            Searchcount++;
            return (*o1).Artist.compare((*o2).Artist);
        }

    };

    //Compares the title of one song to another.
    class TitleComparator {

    public:

        int compare(Song arg0, Song arg1) {

            string Title1 = arg0.Title;
            string Title2 = arg1.Title;
            return Title1.compare(Title2);
        }
    };


public:

    //Testing method to make sure the Song class works and 
    //the compareTo method works.


    string getArtist(){
        return Artist;
    };

    string getTitle(){
        return Title;
    };

    string getLyrics(){
        return Lyrics;
    };

    string toString(){
        return Artist + ", " + Title + ", " + Lyrics;
    };

    //compareTo method used for sorting songs.
    //increments Sortcount each time it is called to keep track
    //of the efficiency of the sort algorithm.
    int compareTo(Song other){
        Sortcount++;
        int art = Artist.compare(other.Artist);
        if (art == 0){

            return Title.compare(other.Title);
        }
        else
            return art;
    }
};

int main(int argc, char** argv){
    Song test1 = Song("cat", "bat", "this is not a real song");
    Song test2 = Song("cat", "apple", "also not a real song");
    int compareResult = test1.compareTo(test2);
    if (compareResult == -1){
        std::cout << "test1 comes first";
    }
    else{
        cout << "test2 comes first";
        cout << test2.toString();
    }
    getchar();
    return 0;
}

changes done:

  1. Moved main() outside the class
  2. Made compareTo() function public as it was being directly called
  3. Removed static from int Sortcount, as i could not update the variable without that.
  • *...as only static functions can make changes to static variables* this is not true. The reason it fails when `SortCount` is static is because both static variables are only declared but not defined. The **declaration** of a static member variable is `static int SortCount;` inside the class definition. The **definition** of a static member variable is `int Song::SortCount = 0;` appearing in a single source file outside the class definition. Adding only the declaration and forgetting the definition leads to a linking error. This doesn't happen for the other static member because it's not used. – Ionut Apr 01 '15 at 09:12
  • I saw, but the right fix is to add the definitions for those static variables, not remove the `static` qualifier. Removing the `static` changes the behavior of the code. – Ionut Apr 01 '15 at 09:22
  • I dont know how to do it. This is new to me as well. – Ganesh Kamath - 'Code Frenzy' Apr 01 '15 at 09:27
  • `int Song::SortCount = 0;` outside the class definition (right before `main` for example). – Ionut Apr 01 '15 at 09:28