gpt4 book ai didi

c++ - 类错误

转载 作者:行者123 更新时间:2023-11-28 02:45:22 26 4
gpt4 key购买 nike

我创建了这段代码,但在最后的 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>')

最佳答案

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

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

代替

cout<<emp3.getSalaried<<endl;

关于c++ - 类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com