类错误

时间:2014-07-07 16:11:48

标签: c++ class

我创建了此代码,我在cout的最终main中遇到错误。仅当我尝试从class Manager打印内容时才会出现此错误。我在Stackoverflow上搜索了同样的错误,我不得不说很多都有这个错误,但主要是在模板上,这就是为什么我不能在我的情况下采用任何这些解决方案。

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//Base Class
class Employee {
    private:
        string name;
        double pay;
    public:
        Employee()
        {
            name ="";
            pay = 0;
        }

        Employee(string empName, double payRate){
            name = empName;
            pay = payRate;
        }

        string getName(){
            return name;
        }

        void setName(string empName){
            name = empName;
        }

        double getPay()
        {
            return pay;
        }

        void setPay(double payRate)
        {
            pay = payRate;
        }

        string toString()
        {
            stringstream stm;
            stm <<name<<": "<< pay;
            return stm.str();
        }
};


//derived class

class Manager : public Employee
{
    private:
        bool salaried;

    public:
        Manager(string name, double payRate, bool isSalaried)
           : Employee(name, payRate)
           {
               salaried = isSalaried;
           }

           bool getSalaried()
           {
               return salaried;
           }

};

int main()
{
    Employee emp1("Jane",3500);
    Employee emp2("Bill",3200);
    cout<<emp1.toString()<<endl;
    cout<<emp2.toString()<<endl;

    Manager emp3("Bob",1500,true);
    cout<<emp3.toString()<<endl;
    cout<<emp3.getSalaried<<endl;

    return 0;
}

错误:  no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and '<unresolved overloaded function type>')

1 个答案:

答案 0 :(得分:2)

getSalaried中的函数调用后忘记了括号,应该是

cout<<emp3.getSalaried()<<endl;

而不是

cout<<emp3.getSalaried<<endl;