该代码为什么会报错:class People { int f(int m, int n) { return 100; } public int f(int x,int y){ return x+y; } }
时间: 2023-08-21 17:30:26 浏览: 70
在该代码中,定义了两个同名但参数列表不同的 `f` 方法,这违反了 Java 中方法的重载规则。在 Java 中,方法的重载要求方法名相同,但参数列表必须不同。参数列表不同指的是参数个数不同或参数类型不同,不包括参数的名称和参数的顺序。在这个例子中,两个 `f` 方法的方法名相同,但参数列表相同,都是 `(int, int)`,因此会导致编译错误,Java 编译器无法确定要调用哪个方法。要解决这个问题,可以修改其中一个方法的参数列表,或者使用不同的方法名。例如:
```
class People {
int f(int m, int n) {
return 100;
}
public int f(int x, int y, int z) {
return x + y + z;
}
}
```
或者:
```
class People {
int f(int m, int n) {
return 100;
}
public int g(int x, int y) {
return x + y;
}
}
```
这样就可以避免方法重载导致的编译错误。
相关问题
已知类People的定义如下: class Peoplel People(int n){ People(double m) int People(int n) return 100; void People0)f 则叙述错误的是()
题目中没有提到 `People` 类的定义,只给出了三个可能的构造函数声明和一个成员函数声明。因此,无法确定哪个选项是错误的。
但是,`People` 类的定义应该是以下之一:
```cpp
class People {
public:
People(int n);
People(double m);
int People(int n);
void People0();
};
```
或
```cpp
class People {
public:
People(int n) {}
People(double m) {}
int People(int n) { return 100; }
void People0() {}
};
```
因此,无法确定哪个选项是错误的。
#include<iostream> using namespace std; class Fix{ public: Fix(){}; double money()const{return 0.0;} }; class Baseclass1{ public: Baseclass1(int x){ N=x; cout<<"The number of this people is:"<<N<<endl; } private: int N; }; class Baseclass2{ public: Baseclass2(string z){ A=z; cout<<"The name of this people is:"<<A<<endl; } private: string A; }; class Baseclass3{ public: Baseclass3(double money,double hour,double sell){ } private: double Money,Hour,Sell; }; class Manager:public Baseclass1,public Baseclass2,public Baseclass3,public Fix{ public:Manager(); Manager(int x);Manager(string x); double money()const{return 8000.0;} }; Manager::Manager(int x):Baseclass1(x){ } Manager::Manager(string x):Baseclass2(z){ } class Tech:public Baseclass1,public Baseclass2,public Baseclass3,public Fix{ public:Tech(); Tech(int x);Tech(string x); double money()const{return 100*H;} private: double H; }; Tech::Tech(int x):Baseclass1(x){ } Tech::Tech(string x):Baseclass2(z){ } class Seller:public Baseclass1,public Baseclass2,public Baseclass3,public Fix{ public: Seller(); Seller(int x);Seller(string x); double money()const{return Sell*0.04;} private: double Sell; }; Seller::Seller(int x):Baseclass1(x){ } Seller::Seller(string x):Baseclass2(z){ } class sema:public Baseclass1,public Baseclass2,public Baseclass3,public Fix{ public: sema(); sema(int x);sema(string x); double money()const{return 5000+Sell*0.05;} private: double Sell; }; sema::sema(int x):Baseclass1(x){ } sema::sema(string x):Baseclass2(z){ } int main(){ Manager q; double money; money=q.money(); Manager x(10000); Manager z(Tom); cin>>Sell>>endl; cin>>Hour>>endl; }帮我改一下错
这段代码存在多处错误,我来一一指出并做出修改:
1. 在 Manager 类和 Tech 类中的 Baseclass3 构造函数中没有对类的成员变量进行赋值,应该修改为:
```
Baseclass3(double money,double hour,double sell): Money(money), Hour(hour), Sell(sell) {}
```
2. 在 Manager 类和 Tech 类中的构造函数中,没有对类的成员变量进行初始化,应该添加以下语句:
```
Manager(int x) : Baseclass1(x), Baseclass2(""), Baseclass3(0.0, 0.0, 0.0), Fix() {}
Manager(string x) : Baseclass1(0), Baseclass2(x), Baseclass3(0.0, 0.0, 0.0), Fix() {}
Tech(int x) : Baseclass1(x), Baseclass2(""), Baseclass3(0.0, 0.0, 0.0), Fix() {}
Tech(string x) : Baseclass1(0), Baseclass2(x), Baseclass3(0.0, 0.0, 0.0), Fix() {}
```
3. 在 main 函数中,没有定义变量 Sell 和 Hour,应该先定义这两个变量,再读入它们的值:
```
double Sell, Hour;
cin >> Sell >> Hour;
```
4. 在 sema 类中,构造函数中的参数名字和类的成员变量名字重复了,应该修改为:
```
sema(int x) : Baseclass1(x), Baseclass2(""), Baseclass3(0.0, 0.0, 0.0), Fix() {}
sema(string x) : Baseclass1(0), Baseclass2(x), Baseclass3(0.0, 0.0, 0.0), Fix() {}
```
最终修改后的代码如下:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)