#include<iostream> using namespace std; #include<string> class Employee { friend ostream& operator<<(ostream& out, Employee& employee); private: string name; int id; double salary; public: Employee(string n, int i, double s):name(n), id(i), salary(s) {}; void setname(string n) { name = n; } void setid(int i) { id = i; } void setsalary(double s) { salary = s; } string getname() { return name; } int getid() { return id; } double getsalary() { return salary; } }; ostream& operator<<(ostream& cout, Employee& employee) { cout << "姓名为" << employee.getname() << endl; cout << "id为" << employee.getid() << endl; cout << "薪水是" << employee.getsalary() << endl; }; int main() { Employee employee("潘仁飞", 15.150); system("pause"); return 0; }哪里有问题
时间: 2024-03-21 13:43:28 浏览: 55
代码中有两处问题:
1.在 `Employee` 类的构造函数中,只传入了两个参数 `"潘仁飞"` 和 `15.15`,然而该构造函数需要三个参数,分别是 `name`、`id` 和 `salary`。因此需要将构造函数中的参数数量改为三个,比如改为 `Employee(string n, int i, double s)`。
2.在 `main` 函数中,没有调用 `operator<<` 函数来输出 `employee` 对象的信息。需要在 `main` 函数中添加如下代码:`cout << employee;`。
修改后的代码如下:
```c++
#include<iostream>
#include<string>
using namespace std;
class Employee {
friend ostream& operator<<(ostream& out, Employee& employee);
private:
string name;
int id;
double salary;
public:
Employee(string n, int i, double s):name(n), id(i), salary(s) {};
void setname(string n)
{
name = n;
}
void setid(int i)
{
id = i;
}
void setsalary(double s)
{
salary = s;
}
string getname()
{
return name;
}
int getid()
{
return id;
}
double getsalary()
{
return salary;
}
};
ostream& operator<<(ostream& cout, Employee& employee) {
cout << "姓名为" << employee.getname() << endl;
cout << "id为" << employee.getid() << endl;
cout << "薪水是" << employee.getsalary() << endl;
};
int main() {
Employee employee("潘仁飞", 1, 15.15);
cout << employee;
system("pause");
return 0;
}
```
阅读全文