We learned about classes in my class and were told to basically convert a previous assignment so that it uses classes. I can't figure out how to use my function to access my private members though. From what I understand, my function has to be a public member to access my private members but I'm still getting errors on that front. Any help/explanation is greatly appreciated.
class Song
{
public:
void loadsongs(Song song[], int &i, const int ALBUM_SIZE, char buffer[],int maxsong, int listsize, char *pch, char Title[], char Artist[], char Album[], int Min, int Sec);
private:
char Title[MAX_CHAR];
char Artist[MAX_CHAR];
char Album[MAX_CHAR];
int Min;
int Sec;
};
int main()
{
const int ALBUM_SIZE=100;
Song song[ALBUM_SIZE];
char buffer[MAX_CHAR];
char *pch;
int i;
int maxsong;
int listsize;
i=0;
maxsong=0;
Song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
cout<<song[1].Title<<endl;
}
void loadsongs(Song song[], int &i, const int ALBUM_SIZE, char buffer[], int maxsong, int listsize, char *pch, char Title[], char Artist[], char Album[], int Min, int Sec)
{
ifstream in;
in.open("music.txt");
testy.cpp: In function ‘int main()’:
testy.cpp:36:6: error: expected unqualified-id before ‘.’ token
Song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
^
testy.cpp: In function ‘void loadsongs(Song*, int&, int, char*, int, int, char*, char*, char*, char*, int, int)’:
testy.cpp:64:32: error: ‘char Song::Title [101]’ is private within this context
strcpy(song[i].Title, temp[0]);
^~~~~
testy.cpp:14:28: note: declared private here
char Title[MAX_CHAR];
^
testy.cpp:65:32: error: ‘char Song::Artist [101]’ is private within this context
strcpy(song[i].Artist, temp[1]);
^~~~~~
testy.cpp:15:29: note: declared private here
char Artist[MAX_CHAR];
^
testy.cpp:66:32: error: ‘char Song::Album [101]’ is private within this context
strcpy(song[i].Album, temp[2]);
^~~~~
testy.cpp:16:28: note: declared private here
char Album[MAX_CHAR];
^
testy.cpp:68:25: error: ‘int Song::Min’ is private within this context
song[i].Min=atoi(temp[3]);
^~~
testy.cpp:17:13: note: declared private here
int Min;
^~~
testy.cpp:69:25: error: ‘int Song::Sec’ is private within this context
song[i].Sec=atoi(temp[4]);
^~~
testy.cpp:18:13: note: declared private here
int Sec;
^~~
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
usingnamespace std;
constint MAX_CHAR=101;
class Song
{
public:
void loadsongs(Song song[], int &i, constint ALBUM_SIZE, char buffer[],int maxsong, int listsize, char *pch, char Title[], char Artist[], char Album[], int Min, int Sec);
private:
char Title[MAX_CHAR];
char Artist[MAX_CHAR];
char Album[MAX_CHAR];
int Min;
int Sec;
};
int main()
{
constint ALBUM_SIZE=100;
Song song[ALBUM_SIZE];
char buffer[MAX_CHAR];
char *pch;
int i;
int maxsong;
int listsize;
i=0;
maxsong=0;
Song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
cout<<song[1].Title<<endl;
}
void loadsongs(Song song[], int &i, constint ALBUM_SIZE, char buffer[], int maxsong, int listsize, char *pch, char Title[], char Artist[], char Album[], int Min, int Sec)
{
ifstream in;
in.open("music.txt");
if(!in)
{
exit(1);
}
while( in.getline(buffer, MAX_CHAR) )
{
char temp[5][MAX_CHAR];
int counter;
counter=0;
pch=strtok (buffer,";");
while(pch!=nullptr)
{
strcpy(temp[counter], pch);
pch = strtok (nullptr, ";");
counter++;
}
strcpy(song[i].Title, temp[0]);
strcpy(song[i].Artist, temp[1]);
strcpy(song[i].Album, temp[2]);
song[i].Min=atoi(temp[3]);
song[i].Sec=atoi(temp[4]);
i++;
maxsong++;
}
in.close();
}
There are a few problems here. Number 1: Song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
This is wrong. Remember, "Song" is a class type, not an object. In order to access loadsongs, you need an object of type Song:
1 2
Song mySong;
mySong.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
Secondly, change your loadsongs declaration to this: void loadsongs(Song song[], int &i, constint ALBUM_SIZE, char buffer[],int maxsong, int listsize, char *pch);
You do not need the additional parameters to pass in the private data members. Since loadsongs() is a member function of your class, it will always have access to those private data members without having to pass it in directly. This is because a member function works on a specific object of the class. The only time you would have to pass a private member as a parameter to a member function is if it is static.
Secondly, you CANNOT access the private data members Title, Artist, Album, Min, and Sec outside of your class member functions (whether they are private or public doesn't matter). You try to access them in the same line:
Song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch, Title, Artist, Album, Min, Sec );
This code is in main(). main() knows nothing about these members because it is not part of your class. You can only use these if you are inside of a member function. Also, as I said previously, you do NOT need to pass these in as a parameter anyways because once you are within the loadsongs() function, it has access to the private data members internally.
To summarize:
1.) create an object of type Song to call loadsongs on.
2.) get rid of those extra unnecessary parameters
3.) do not try to use names of private data members outside of the body of a class member function.
The second problem is that you are getting a bunch of errors due to your definition of class member functions. You need to specify that it is a class member, otherwise the compiler will assume you are just declaring a global namespace scope function:
1 2 3 4 5
//Define like this:
void Song::loadsongs(/*...*/)
{
//...
}
Thank you and I didn't notice that source code tag before so I'll use that from now on. I'm still confused on the class object though. I created my Song object called song[ALBUM_SIZE] but i get this error.
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
usingnamespace std;
constint MAX_CHAR=101;
class Song
{
public:
void loadsongs(Song song[], int &i, constint ALBUM_SIZE, char buffer[],int maxsong, int listsize, char *pch);
private:
char Title[MAX_CHAR];
char Artist[MAX_CHAR];
char Album[MAX_CHAR];
int Min;
int Sec;
};
int main()
{
constint ALBUM_SIZE=100;
Song song[ALBUM_SIZE];
char buffer[MAX_CHAR];
char *pch;
int i;
int maxsong;
int listsize;
i=0;
maxsong=0;
song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch);
}
void Song::loadsongs(Song song[], int &i, constint ALBUM_SIZE, char buffer[], int maxsong, int listsize, char *pch)
{
ifstream in;
in.open("music.txt");
if(!in)
{
exit(1);
}
while( in.getline(buffer, MAX_CHAR) )
{
char temp[5][MAX_CHAR];
int counter;
counter=0;
pch=strtok (buffer,";");
while(pch!=nullptr)
{
strcpy(temp[counter], pch);
pch = strtok (nullptr, ";");
counter++;
}
strcpy(song[i].Title, temp[0]);
strcpy(song[i].Artist, temp[1]);
strcpy(song[i].Album, temp[2]);
song[i].Min=atoi(temp[3]);
song[i].Sec=atoi(temp[4]);
i++;
maxsong++;
}
in.close();
}
testy.cpp: In function ‘int main()’:
testy.cpp:36:7: error: request for member ‘loadsongs’ in ‘song’, which is of non-class type ‘Song [100]’
song.loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch);
^~~~~~~~~
Isn't song an initialized object of class Song? I've tried song[ALBUM_SIZE] and song[100] as well but no luck.
Here are some indications of problems with loadsongs():
- parameter ALBUM_SIZE is never used.
- loadsongs() doesn't use the value of buffer that's passed in, and main() doesn't use the value that's left in buffer when loadsongs() exits. So why is buffer a parameter?
- Same comment for parameter pch.
- Parameter maxsong is set but not used.
- Parameter listsize isn't used.
An easy way to catch problems like this is the comment your code. For each function or class method, put a comment at the top describing what it does, what each parameter means, and what the return value is. This forces you to think critically about each of these things.
What you really want is a method that reads one song from a stream. Let's call it read() and have it return a bool. Then you want main() to loop through the input file, reading songs until read() fails: