Undeclared Identifiers

I am attempting to write a program that allows the user to add,remove, and display all songs in a playlist. I am new to programming and I am getting errors that I do not know how to fix. I am getting undeclared identifier errors when calling to my functions. It's probably something simple but I don't see it. I have not yet been able to test the program so I do not know if it even works yet, but one problem at a time I guess.
Thanks in advance.


Song.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef SONG_H_                  
#define SONG_H_

#include <string>


using namespace std;

                                
class Song 
{

public:
    
    Song song();
    Song song(string someTitle, string someArtist, string someAlbum);

    Song getTitle (string songTitle);
    Song getArtist (string songArtist);
    Song getAlbum (string songAlbum);
    
    void readFile (Song playlist[], string line, int counter);
    
    void addSong (Song playlist[], int counter);
    void removeSong (Song playlist[], int counter);
    void displaySongs (Song playlist[], int counter); 
    
    void quitProgram();
    
    string songTitle;
    string songArtist;
    string songAlbum;

    
private:
    
};

#endif 



Song.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Song.h"
#include <iostream>
#include <fstream>


Song Song::song()                      
{   
    
    songTitle = "Unknown";
    songArtist = "Unknown";
    songAlbum = "Unknown";
    
}

Song Song::song(string someTitle, string someArtist, string someAlbum)      
{
    songTitle = someTitle;
    songArtist = someArtist;
    songAlbum = someAlbum;
    
    
}





Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
#include <fstream>
#include "Song.h"
#include "Song.cpp"


using namespace std;

Song playlist[200];


int main ()
{
    ifstream inFile; 
    ofstream outFile;
    
    
    string infilePath;
    cout << "Enter the file path for 'songs.txt'.";
    cin >> infilePath;
    
    
    inFile.open(infilePath.c_str(), ifstream::in);                
    if (!inFile)                                                                       
    {                                                                               
        cout << "Can not open input file."<< endl;
        return 1;
    }
    string line;
    getline(inFile, line);
    int counter = 0;
    while (!inFile.eof())                                             
    {
        
        readFile(playlist, line, counter);   //Use of undeclared identifier readFile
        getline(inFile, line);
        counter++;
        
        
    }

    
    char option;
    cout << "Enter the number of the option you which to use:" << endl;
    cout << "1 Add a song." << endl << "2 Remove a song." << endl << "3 Display all songs." << endl << "4 Quit." << endl; 
    cin >> option;
    
    
    switch (option)
    {
        case 1:
            
            addSong (playlist[counter], counter);        //Use of undeclared identify addSong
            counter++;
            break;
            
        case 2:
            
            removeSong (playlist[counter], counter);     //Use of undeclared identifier removeSong
            counter--;
            break;
            
        case 3:
            
            displaySongs (playlist[counter], counter);   //Use of undeclared identifier displaySong
            break;
            
        case 4: 
            
            break;
            
        default:
            cout << "That is not a valid choice." << endl;
            
            return main();
    }        
    

   
    return 0;
}



//*********************************

void Song::readFile(Song playlist[], string line, int counter)
{  
    
    int first = line.find_first_of(':');                                //Take input line then take out the : and create a substring
    playlist[counter].songTitle = line.substr(0, first);
    
    int second = line.find(':', first + 1);
    playlist[counter].songArtist = line.substr(first + 1, second - (first + 1));
    
    int third = line.find(':', second + 1);
    playlist[counter].songAlbum = line.substr(second + 1, third - (second + 1));
    
    
    return;
    
}




void Song::addSong(Song playlist[], int counter)       //function that adds a song to the playlist
{   
    
    cout << "Enter the name of the song you wish to add:" << endl;
    cin >> playlist[counter].songTitle;
    cout << "Enter the name of the artist of the song:" << endl;
    cin >> playlist[counter].songArtist;
    cout << "Enter the album of the song:" << endl;
    cin >> playlist[counter].songAlbum;
    
    return main();
    
}




void Song::removeSong(Song playlist[], int counter)
{   
    
    int indexToRemove;
    cout << "What number song do you wish to remove?" << endl;
    cin >> indexToRemove;
        //decrease the number of elements
    for(int i = indexToRemove; i < counter; i++)
        playlist[i] = playlist[i+1];              //shift elements over to remove element in index 3

    return main();
    
}




void Song::displaySongs(Song playlist[], int counter)
{
    cout << "The current songs on the playlist are:" << endl;
    
    int ticker;
    for (ticker = 0 ; ticker <= counter; ticker++) 
    {
        
        cout << playlist[ticker].songTitle << ':' << playlist[ticker].songArtist << ':'<< playlist[ticker].songAlbum << endl;
        
        
    }
    return main();
    
}



void Song::quitProgram()
{
    //Output the updated song list to songs.txt
    
    
    return;
    
}




Hi.

The first thing I noticed is that your constructors aren't quite right. When declared, they should look more like:
1
2
3
4
//...
Song();
Song(string someTitle, string someArtist, string someAlbum);
//... 

And when implemented, they should look more like...
1
2
3
4
Song::Song()                      
//...
Song::Song(string someTitle, string someArtist, string someAlbum)      
//... 

Notice anything different? First, the capitalization matches the name of the class. Second, constructors don't have a return value. I hope that helps!


---



The second thing that I noticed is that you never declare or define your functions, though they look suspiciously like the ones you declared in your class. Given how you're using them, I would remove them entirely from the class and use them as regular functions. I trust you know how to do that. :)


---



The third thing I noticed is that you can't seem to decide how you want those functions to work. playlist[counter] will return *one* element, but in your declarations...
/*...*/displaySongs(Song playlist[], int counter)
...you expect an array. You'll probably need to decide on that.



-Albatross
Last edited on
So what I take from this is that even though they are declared in the class header file that doesn't count for main? I have to re-declare them? Now when I do remove those functions from the class and instead declaring them in the function I get errors that I have never had problems with before in the function prototypes,
1
2
3
4
void readFile (Song playlist[], string line, int counter);    //For all declarations - Expected ')' and         //Incomplete type void
void addSong (Song playlist[], int counter);
void removeSong (Song playlist[], int counter);
void displaySongs (Song playlist[], int counter);


and in my definitions

1
2
void readFile(Song playlist[], string line, int counter)        //Variable has incomplete type void (x2)                                                                
{    ...}                                                                          //Expected ')' 


Now however in
 
void addSong(Song playlist[], int counter)

There is no error about type void, I am lost at the difference


As for the playlist[] issue, I have changes all my function calls to just playlist. This I presume will return the changed values back into the array as a whole since they are automatically treated as reference parameters? And when I need to display the For function should still be relevant?
You declared your functions in the class. That makes a difference as far as what's in scope and how they're used. Normally, functions are used like:

function_name(arguments);
...but functions declared in classes are used like:
instance_of_a_class.function_name(arguments);

Those errors, however, are strange because not only can't I find a problem with them but when I plugged them into a test file they compiled just fine.

...do you mind pasting your current bits of code? Maybe those declarations are in the wrong place or there was some syntax error before them.

And... if I understand correctly, then yes and yes.

-Albatross
Last edited on
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>
#include <fstream>
#include "Song.h"
#include "Song.cpp"


using namespace std;

class Song playlist[200];

void readFile (Song playlist[], string line, int counter);          //For all declarations - Expected ')' and Incomplete type void
void addSong (Song playlist[], int counter);
void removeSong (Song playlist[], int counter);
void displaySongs (Song playlist[], int counter);
int quitProgram();


 
int main ()
{
    ifstream inFile; 
    ofstream outFile;
    
    
    string infilePath;
    cout << "Enter the file path for 'songs.txt'.";
    cin >> infilePath;
    
    
    inFile.open(infilePath.c_str(), ifstream::in);                
    if (!inFile)                                                                       
    {                                                                               
        cout << "Can not open input file."<< endl;
        return 1;
    }
    string line;
    getline(inFile, line);
    int counter = 0;
    while (!inFile.eof())                                             
    {
        
        readFile(playlist, line, counter);   //Use of undeclared identifier readFile
        getline(inFile, line);
        counter++;
        
        
    }

    
    char option;
    cout << "Enter the number of the option you which to use:" << endl;
    cout << "1 Add a song." << endl << "2 Remove a song." << endl << "3 Display all songs." << endl << "4 Quit." << endl; 
    cin >> option;
    
    
    switch (option)
    {
        case 1: addSong (playlist, counter);        //Use of undeclared identify addSong
                counter++;
                break;
        
            
        case 2: removeSong (playlist, counter);     //Use of undeclared identifier removeSong
                counter--;
                break;
            
        case 3: displaySongs (playlist, counter);   //Use of undeclared identifier displaySong
                break;
            
        case 4: quitProgram();
            
            break;
            
        default:
            cout << "That is not a valid choice." << endl;
            
            return main();
    }        
    

   
    
}



//*********************************

void readFile(Song playlist[], string line, int counter)        //Variable has incomplete type void (x2)                                                                
{                                                               //Expected ')'
    
    int first = line.find_first_of(':');                                //Take input line then take out the : and create a substring
    playlist[counter].songTitle = line.substr(0, first);
    
    int second = line.find(':', first + 1);
    playlist[counter].songArtist = line.substr(first + 1, second - (first + 1));
    
    int third = line.find(':', second + 1);
    playlist[counter].songAlbum = line.substr(second + 1, third - (second + 1));
    
    
    return;
    
}




void addSong(Song playlist[], int counter)       //function that adds a song to the playlist
{   
    
    cout << "Enter the name of the song you wish to add:" << endl;
    cin >> playlist[counter].songTitle;
    cout << "Enter the name of the artist of the song:" << endl;
    cin >> playlist[counter].songArtist;
    cout << "Enter the album of the song:" << endl;
    cin >> playlist[counter].songAlbum;
    
    return;
    
}




void removeSong(Song playlist[], int counter)
{   
    
    int indexToRemove;
    cout << "What number song do you wish to remove?" << endl;
    cin >> indexToRemove;
        //decrease the number of elements
    for(int i = indexToRemove; i < counter; i++)
        playlist[i] = playlist[i+1];              //shift elements over to remove element in index

    return;
    
}




void displaySongs(Song playlist[], int counter)
{
    cout << "The current songs on the playlist are:" << endl;
    
    int ticker;
    for (ticker = 0 ; ticker <= counter; ticker++) 
    {
        
        cout << playlist[ticker].songTitle << ':' << playlist[ticker].songArtist << ':'<< playlist[ticker].songAlbum << endl;
        
        
    }
    return;
    
}



int quitProgram()
{
    //Output the updated song list to songs.txt
    
    
    return 0;
    
}




Song.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef SONG_H_                  
#define SONG_H_

#include <string>


using namespace std;

                                
class Song 
{

public:
    
    Song();
    Song(string someTitle, string someArtist, string someAlbum);

    Song getTitle (string songTitle);
    Song getArtist (string songArtist);
    Song getAlbum (string songAlbum);
    
    void readFile (Song playlist[], string line, int counter);
    void addSong (Song playlist[], int counter);
    void removeSong (Song playlist[], int counter);
    void displaySongs (Song playlist[], int counter); 
    
    int quitProgram();
    
    string songTitle;
    string songArtist;
    string songAlbum;

    
private:
    
};

#endif 




Song.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Song.h"
#include <iostream>
#include <fstream>


string songTitle = "Unknown";
string songArtist = "Unknown";
string songAlbum = "Unknown";

Song Song();                      //Constructor
Song::Song(string someTitle, string someArtist, string someAlbum)      //constructor with parameters
{
    songTitle = someTitle;
    songArtist = someArtist;
    songAlbum = someAlbum;
    
    
}
At line 10, why the class?

But aside from that and aside from the fact that you never implemented your default constructor (the one without parameters) which is still a bit wonky, I'm not sure what's wrong. Probably I'm missing something obvious.

-Albatross
Alright, thanks for the help!
Are you on the same course as John?
Topic archived. No new replies allowed.