2013-05-14 58 views
0

嗨我有這個c + +項目,這讓我一些奇怪的錯誤,我不知道如何解決。所以如果有人能給我一個解決方案,那會很好。c + +運算符不明確和其他

#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; 
     is >> 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; 
      for (int i=0;i<5;i++) 
      { 
       resultVector.push_back(0); 
      } 
      for (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; 
      for (int i=0;i<m_vData.size();i++) 
      { 
       sum+=m_vData[i].GetMinutes(); 
      } 
      return sum/m_vData.size(); 
     } 
    }; 

    ostream& operator<<(ostream& os, CAnalizeTime &obj) 
    { 
     for (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; 
    } 

我得到這個錯誤,當我在VC6 error C2593: 'operator >>' is ambiguous運行它,當我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> All outputs are 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 ========== 
上運行它,我得到這個錯誤

我的問題是什麼原因造成這種錯誤

我在這裏有錯誤:

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); 
      } 
     } 

這是什麼錯誤消息E視象:

--------------------Configuration: project_testing - Win32 Debug-------------------- 
Compiling... 
project_testing.cpp 
D:\My Documents\project_testing\project_testing\project_testing.cpp(143) : error C2593: 'operator >>' is ambiguous 
D:\My Documents\project_testing\project_testing\project_testing.cpp(156) : error C2593: 'operator >>' is ambiguous 
Error executing cl.exe. 

project_testing.obj - 2 error(s), 0 warning(s) 
+1

您的代碼中沒有有效的'main'。 – talonmies

+0

你能更具體一點嗎 – user2374907

+3

'void main'在C++中是無效的。它應該是'int main' –

回答

2

的問題是,obj.getEmployAt(i);回報右值臨時CStudentEmploy對象,然後發送給operator<< overlaod。但是操作員超載期望參考並且不能綁定到右值。

你將不得不採取一個const引用,而不是

ostream& operator<<(ostream& os, const CStudentEmploy &obj) { 
           ^^^^^ 

並修復它使用的功能作爲const例如

string GetName() const { return m_strName; } 
       ^^^^^ 


或者你可以解決您的getEmployAt函數返回參考代替。

CStudentEmploy& getEmployAt(int i) { return m_vData[i]; } 
      ^^^ 


正如在評論中提到:我不知道你用的是什麼編譯器,但void不是main有效的返回值。使用int mainreturn 0;

+0

它固定在兩三個曖昧的錯誤,我在這裏有此錯誤,以及: \t CAnalizeTime() \t { \t \t ifstream的FS; \t \t fs.open(「test.txt」); \t \t if(!fs.is_open())cout <<「錯誤打開文件!\ n「; \t \t CStudentEmploy聘用;(!fs.eof()) \t \t而 \t \t { \t \t \t FS >>聘用; \t \t \t加(聘請); \t \t} \t } \t \t CAnalizeTime(const string&strFileName) \t { \t \t ifstream fs; \t \t fs.open(strFileName.c_str()); \t \t if(!fs.is_open())cout <<「錯誤打開文件!\ n」; \t \t CStudentEmploy聘請; (!fs.eof()) \t \t而 \t \t { \t \t \t FS >>聘用; \t \t \t add(employ); \t \t} \t} – user2374907

+0

@ user2374907你可以把錯誤作爲編輯你的問題。什麼是「期限」? – stardust

+0

@ user2374907你可以把它放在你的問題。閱讀評論並不困難。 – stardust