c ++运算符是模糊的和其他

时间:2013-05-14 19:03:48

标签: c++ visual-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);
            }
        }

这是错误消息的样子:

--------------------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 个答案:

答案 0 :(得分:2)

问题是obj.getEmployAt(i);会返回您发送到CStudentEmploy重叠的右值临时operator<<对象。但是运算符重载需要引用,并且不能绑定到右值。

您将不得不采用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;