Can someone please tell me what is wrong with this?
This is what I have to do and the driver was given.
The first step is to create a C++ class for a song. Each song contains private data consisting of a title, artist
and size in megabytes. The title and artist are strings, and the size has type int. The song class must have
a default constructor, a constructor that initializes each of the data members, get and set functions for
each data member, and an overloaded cout operator. The class declaration is in file Song.h and the function
implementations are in Song.cpp. The class must work properly with the driver posted on the class website.
i'm not sure if the code was copy/pasted into here, but if so, line 11 (strcpy ( artist, a )) is missing a semicolon, which can often greatly confuse compilers, and not always produce a useful error. most compilers will also complain if you do not make sure there is an empty line at the end of your file. good luck.
your welcome, glad i could help. good point jsmith! @cannsyl: when you are within a class declaration, it is pretty much exactly like a normal function prototype return_type function_name (parameters);. scope resolution is unnecessary since you are within the class definition. as to overloading c++ operators is actually almost exactly like writing a member function for a class, this should be the info you need: <http://www.cplusplus.com/doc/tutorial/classes2.html >
Its never a good idea to just add friends to classes. Theres a different way to overload to operator.
Ex
1 2 3 4 5 6 7 8 9 10
// inside class
void display(ostream& o) {
// FORMAT OF DISPLAY
}
// outside class
ostream& operator<<(ostream& o, Song& s) {
s.display(o);
return o;
}
Its an alternative to using the friend function, does the same thing.
I think your error comes from the .cpp file: Song ostream& operator<< (ostream& os, const Song& obj);
which needs to be: ostream& Song::operator<< (ostream& os, const Song& obj);
In general, widespread use of friends is a sign of incorrect encapsulation. However,
when talking about operators, its hard to say whether operators should be members or friends. For example, the boost operators library declares all of the operators to be friend functions.