I have an operator overloading question...
I have attempted to overload the >> operator so I can
read a file directly into a "SongInfo" object, and attempted to
overload the << operator so I can display a "SongInfo"
object directly to dos console. When attempting to compile, I
recieve, "overloaded operator must take exactly one argument.." errors for both insertion and extraction operators. My code seems to follow closely examples out of my book which clearly shows these
operators taking 2 arguments. What am I doing wrong?
Using DevCpp
Overloaded operators are used in the load_file() and view_tracks() functions.
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
struct SongInfo
{
int track_no,
track_pos,
track_length,
runtime;
SongInfo *next;
[b]ifstream& operator>> (ifstream &ins, SongInfo *target);
ostream& operator<< (ostream &outs, SongInfo *target);[/b]
};
class CDmanager
{
public:
CDmanager();
~CDmanager();
void load_file();
void set_runtime(SongInfo*);
void view_tracks();
private:
int current_runtime;
ifstream infile;
SongInfo *head_ptr;
};
int main()
{
CDmanager CD;
CD.load_file();
CD.view_tracks();
return 0;
}
/////////////////////////////////
/// Function Definitions //////
///////////////////////////////
[b]SongInfo& SongInfo::operator>> (ifstream &ins, SongInfo *target)
{
ins >> target->track_no >> target->track_pos >> target->track_len_min
>> target->track_len_sec;
return ins;
}
SongInfo& SongInfo::operator<< (ostream &outs, SongInfo *source)
{
outs << temp->track_no << setw(10)
<< temp->track_pos << setw(10)
<< temp->track_len_min << ':'
<< temp->track_len_sec << setw(10)
<< temp->runtime/60 << ':'
<< temp->runtime%60 << setw(10);
return outs;
}[/b]
CDmanager::CDmanager()
{
current_runtime = 0;
infile.clear();
head_ptr = NULL;
}
CDmanager::~CDmanager()
{
SongInfo *temp = NULL;
while(head_ptr)
{
temp = head_ptr->next;
delete head_ptr;
head_ptr = temp;
}
}
void CDmanager::load_file()
{
SongInfo *temp = head_ptr;
infile.open("C:\\songs.dat");
if(infile.fail())
{
cout << "\n\n\t\aError Opening File! Program Will Now Terminate.";
cout << "\n\n\tPress [Enter] to continue... ";
cin.get();
exit(1);
}
[b]while(infile >> temp)[/b]
{
set_runtime(temp);
temp->next = new SongInfo;
temp = temp->next;
temp->next = NULL;
}
infile.close();
}
void CDmanager::set_runtime(SongInfo *temp)
{
current_runtime += temp->track_length;
temp->runtime = current_runtime;
}
void CDmanager::view_tracks()
{
SongInfo *temp = head_ptr;
cout << "\n\nTrack" << setw(10) << "Song No." << setw(10)
<< "Song Length" << setw(10) << "Cumulative Runtime";
cout << "\n-----" << setw(10) << "--------" << setw(10)
<< "-----------" << setw(10) << "------------------";
while(temp)
{
[b]cout << endl << temp;[/b]
temp = temp->next;
}
cout << "\n\n\tTotal Runtime is: "
<< current_runtime/60 << ':'
<< current_runtime%60;
}
any help is greatly appreciated.