dukemarlon
asked on
operator overloading question
ok, I will poost my code below. I get these two errors:
(54) : error C2601: '>>' : local function definitions are illegal
(54) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
These errors begin at the open bracket after this line:
ifstream & operator>> (ifstream& stream, employee& p)
I'm not sure what to do. Here is my code:
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
employee::employee();
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
(54) : error C2601: '>>' : local function definitions are illegal
(54) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
These errors begin at the open bracket after this line:
ifstream & operator>> (ifstream& stream, employee& p)
I'm not sure what to do. Here is my code:
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
employee::employee();
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
Your operator is defined embedded in the main function. I have not comiled it, but this is definitely a problem. Also, the line employee::employee(); is wrong. The definition of your employee class is incomplete: All the methods that you reference in the operator are not defined. Try to correct these problems, and if it's still not working, post a comment.
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
Your operator is defined embedded in the main function. I have not comiled it, but this is definitely a problem. Also, the line employee::employee(); is wrong. The definition of your employee class is incomplete: All the methods that you reference in the operator are not defined. Try to correct these problems, and if it's still not working, post a comment.
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
Your operator is defined embedded in the main function. I have not comiled it, but this is definitely a problem. Also, the line employee::employee(); is wrong. The definition of your employee class is incomplete: All the methods that you reference in the operator are not defined. Try to correct these problems, and if it's still not working, post a comment.
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}
... so it did work :-) Sorry about that. My browser timed out three times, so I resubmitted the comment a number of times.
ASKER
it kinda fixed it, but now inside of the overloaded function, can't figure out how to set values to any of the objects, I was barraged with errors, and can't figure most of them out.
ASKER
here is what I have, but every object in the overloaded function says it is an illeagal reference
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
employee employee();
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee::compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos)
{
if(hos=='h'||'H')gpay=hour s*payrate* 100;
if(hos=='s'||'S')gpay=payr ate/26*100 ;
fed=(gpay*26)-(2500*depend ents)*(.28 /.26)*100;
state1=gpay*.048*100;
social=gpay*.075*100;
return (gpay, fed, state1, social);
}
ifstream & operator>> (ifstream& stream, employee&)
{
char tmp[80];
stream.getline(tmp, 80, '|');
employee::fname=tmp;
stream.getline(tmp, 80, '|');
employee::lname=tmp;
stream.getline(tmp, 80, '|');
employee::address=tmp;
stream.getline(tmp, 80, '|');
employee::city=tmp;
stream.getline(tmp, 80, '|');
employee::state=tmp;
stream.getline(tmp, 80, '|');
employee::zip=tmp;
stream.getline(tmp, 80, '|');
employee::hos=tmp;
stream.getline(tmp, 80, '|');
employee::payrate=tmp;
stream.getline(tmp, 80, '|');
employee::hours=tmp;
stream.getline(tmp, 80, '|');
employee::dependents=tmp;
return stream;
}
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
employee employee();
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee::compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos)
{
if(hos=='h'||'H')gpay=hour
if(hos=='s'||'S')gpay=payr
fed=(gpay*26)-(2500*depend
state1=gpay*.048*100;
social=gpay*.075*100;
return (gpay, fed, state1, social);
}
ifstream & operator>> (ifstream& stream, employee&)
{
char tmp[80];
stream.getline(tmp, 80, '|');
employee::fname=tmp;
stream.getline(tmp, 80, '|');
employee::lname=tmp;
stream.getline(tmp, 80, '|');
employee::address=tmp;
stream.getline(tmp, 80, '|');
employee::city=tmp;
stream.getline(tmp, 80, '|');
employee::state=tmp;
stream.getline(tmp, 80, '|');
employee::zip=tmp;
stream.getline(tmp, 80, '|');
employee::hos=tmp;
stream.getline(tmp, 80, '|');
employee::payrate=tmp;
stream.getline(tmp, 80, '|');
employee::hours=tmp;
stream.getline(tmp, 80, '|');
employee::dependents=tmp;
return stream;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this:
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class employee
{
public:
friend ifstream &operator>>(ifstream& stream, employee& p);
string lname;
string fname;
string address;
string city;
string state;
string zip;
char hos;
int payrate;
int hours;
int dependents;
int gpay;
int fed;
int state1;
int social;
int net;
int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
};
int main()
{
char in[20];
char out[20];
char str[1024], last;
cout<<"Enter the name of the file you want to write to."<<endl;
cin.get(out, 20);
cin.ignore(80, '\n');
cout<<"Enter the name of the file you want to read from."<<endl;
cin.get(in, 20);
cin.ignore(80, '\n');
ifstream infile;
infile.open(in);
ofstream outfile;
outfile.open(out);
employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
infile.close();
outfile.close();
return 0;
}
ifstream & operator>> (ifstream& stream, employee& p)
{
char tmp[80];
stream.getline(tmp, 80, '|');
p.setfname(tmp);
stream.getline(tmp, 80, '|');
p.setlname(tmp);
stream.getline(tmp, 80, '|');
p.setaddress(tmp);
stream.getline(tmp, 80, '|');
p.setcity(tmp);
stream.getline(tmp, 80, '|');
p.setstate(tmp);
stream.getline(tmp, 80, '|');
p.setzip(tmp);
stream.getline(tmp, 80, '|');
p.sethos(tmp);
stream.getline(tmp, 80, '|');
p.setpayrate(tmp);
stream.getline(tmp, 80, '|');
p.sethours(tmp);
stream.getline(tmp, 80, '|');
p.setdependents(tmp);
return stream;
}