visual c++ - c++ operator is ambiguous and other -
hi have c++ project gets me weird error have no idea how fix. if can give me solution, great.
#include <iostream> #include <string> #include <fstream> #include <vector> #include <algorithm> #include <iomanip> using namespace std; using std::ostream; using std::endl; using std::cout; class cstudentemploy { private: string m_strname; string m_strfacnum; int m_iminutes; public: cstudentemploy(int m = 0) // podrazbirasht se konstruktor { m_strname = "n/a"; m_strfacnum = "n/a"; m_iminutes = m; } cstudentemploy(string n, string f, int m) // ekspliciten konstruktor { m_strname = n; m_strfacnum = f; m_iminutes = m; } cstudentemploy(const cstudentemploy &obj) // copy konstruktor { m_strname = obj.m_strname; m_strfacnum = obj.m_strfacnum; m_iminutes = obj.m_iminutes; } string getname() { return m_strname; } string getfacnum() { return m_strfacnum; } int getminutes() { return m_iminutes; } void setname (string n) { m_strname = n; } void setfacnum (string f) { m_strfacnum = f; } void setminutes (int m) { m_iminutes = m; } cstudentemploy operator =(cstudentemploy obj) { m_strname = obj.m_strname; m_strfacnum = obj.m_strfacnum; m_iminutes = obj.m_iminutes; return *this; } bool operator < (const cstudentemploy& obj) const { return m_iminutes < obj.m_iminutes; } cstudentemploy operator +(const cstudentemploy &obj) const { return cstudentemploy(m_iminutes + obj.m_iminutes); } friend ostream& operator <<(ostream& os, cstudentemploy &obj); friend istream& operator>>(istream& is, cstudentemploy &obj); }; ostream& operator<<(ostream& os, cstudentemploy &obj) { os<<setiosflags(ios::left)<<setw(8)<<obj.getname() <<"|"<<setw(11)<<obj.getfacnum() <<"|"<<setw(8)<<obj.getminutes()<<endl; return os; } istream& operator>>(istream& is, cstudentemploy &obj) { string tmp_strname; string tmp_strfacnum; int tmp_iminutes; >> tmp_strname >> tmp_strfacnum >> tmp_iminutes; obj.setname(tmp_strname); obj.setfacnum(tmp_strfacnum); obj.setminutes(tmp_iminutes); return is; } class canalizetime { private: vector<cstudentemploy>m_vdata; void add(cstudentemploy employ) { m_vdata.push_back(employ); } public: cstudentemploy getemployat(int i) { return m_vdata[i]; } long getemploycount() { return m_vdata.size(); } canalizetime() { ifstream fs; fs.open("test.txt"); if(!fs.is_open()) cout<<"error opening file!\n"; cstudentemploy employ; while(!fs.eof()) { fs>>employ; add(employ); } } canalizetime(const string& strfilename) { ifstream fs; fs.open(strfilename.c_str()); if(!fs.is_open()) cout<<"error opening file!\n"; cstudentemploy employ; while(!fs.eof()) { fs>>employ; add(employ); } } void sort() { sort(m_vdata.begin(),m_vdata.end()); } vector<int> calcnums(int ir1,int ir2,int ir3, int ir4,int ir5,int ir6) { vector<int> resultvector; (int i=0;i<5;i++) { resultvector.push_back(0); } (i=0;i<m_vdata.size();i++) { if(m_vdata[i].getminutes()>=ir1 &&m_vdata[i].getminutes()<ir2) resultvector[0]++;//[ir1-ir2) if(m_vdata[i].getminutes()>=ir2 &&m_vdata[i].getminutes()<ir3) resultvector[1]++;//[ir2-ir3) if(m_vdata[i].getminutes()>=ir3 &&m_vdata[i].getminutes()<ir4) resultvector[2]++;//[ir3-ir4) if(m_vdata[i].getminutes()>=ir4 &&m_vdata[i].getminutes()<ir5) resultvector[3]++;//[ir4-ir5) if(m_vdata[i].getminutes()>=ir5 &&m_vdata[i].getminutes()<ir6) resultvector[4]++;//[ir5-ir6) } return resultvector; } double calcmean() { double sum=0; (int i=0;i<m_vdata.size();i++) { sum+=m_vdata[i].getminutes(); } return sum/m_vdata.size(); } }; ostream& operator<<(ostream& os, canalizetime &obj) { (int i=0;i<obj.getemploycount();i++) { cout<<obj.getemployat(i); } return os; } void main() { canalizetime mytimeanalyzer; //mytimeanalyzer.sort(); cout<<setiosflags(ios::left)<<setw(8) <<"name"<<setw(12) <<"|facnum"<<setw(8)<<"|minutes"<<endl; cout<<"----------------------------"<<endl; cout<<mytimeanalyzer; cout<<"calcmean result:"<<mytimeanalyzer.calcmean()<<endl; vector<int>mycalcnums = mytimeanalyzer.calcnums(1,50,100,200,400,800); cout<<"calcnums result:" <<mycalcnums[0]<<"," <<mycalcnums[1]<<"," <<mycalcnums[2]<<"," <<mycalcnums[3]<<"," <<mycalcnums[4]<<endl; system("pause"); return; }
i'm getting error when run on vc6 error c2593: 'operator >>' ambiguous
, , error when run on 2010
1>------ build started: project: test_project, configuration: debug win32 ------ 1>build started 5/14/2013 10:02:29 pm. 1>initializebuildstatus: 1> touching "debug\test_project.unsuccessfulbuild". 1>manifestresourcecompile: 1> outputs up-to-date. 1>link : error lnk2001: unresolved external symbol _maincrtstartup 1>c:\users\user\desktop\test_project\test_project\debug\test_project.exe : fatal error lnk1120: 1 unresolved externals 1> 1>build failed. 1> 1>time elapsed 00:00:00.80 ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
my question causing errors
i have errors here on these lines:
canalizetime() { ifstream fs; fs.open("test.txt"); if(!fs.is_open()) cout<<"error opening file!\n"; cstudentemploy employ; while(!fs.eof()) { fs>>employ; add(employ); } } canalizetime(const string& strfilename) { ifstream fs; fs.open(strfilename.c_str()); if(!fs.is_open()) cout<<"error opening file!\n"; cstudentemploy employ; while(!fs.eof()) { fs>>employ; add(employ); } }
this error message looks like:
--------------------configuration: project_testing - win32 debug-------------------- compiling... project_testing.cpp d:\my documents\project_testing\project_testing\project_testing.cpp(143) : error c2593: 'operator >>' ambiguous d:\my documents\project_testing\project_testing\project_testing.cpp(156) : error c2593: 'operator >>' ambiguous error executing cl.exe. project_testing.obj - 2 error(s), 0 warning(s)
the problem obj.getemployat(i);
returns rvalue temporary cstudentemploy
object send operator<<
overlaod. operator overload expects a reference , can not bind rvalue.
you have take const reference instead
ostream& operator<<(ostream& os, const cstudentemploy &obj) { ^^^^^
and fix functions used in const
example
string getname() const { return m_strname; } ^^^^^
or can fix getemployat
function return reference instead.
cstudentemploy& getemployat(int i) { return m_vdata[i]; } ^^^
as mentioned in comments: i don't know compiler using void
not valid return value main
. use int main
, return 0;
Comments
Post a Comment