C++ 程序错误分析:构造函数重载参数类型不一致
以下是一个 C++ 程序,程序中存在构造函数重载时参数类型不一致的问题,导致编译器无法确定使用哪一个构造函数。
#pragma once
#include<iostream>
#include<string.h>
using namespace std;
enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
class CPU {
private:
CPU_Rank rank;
int CPU_frequency;
double CPU_voltage;
public:
CPU(int newRank, int newFrequency, int newVoltage)
{
rank = (CPU_Rank)newRank;
CPU_frequency = newFrequency;
CPU_voltage = newVoltage;
};
CPU()//构造函数重载
{
rank = P6;
CPU_frequency = 3000;
CPU_voltage = 220;
}
CPU(int b)
{
CPU_frequency = b;
}
~CPU()
{
cout << 'Good bye!' << endl;
}
CPU(const CPU& p)
{
rank = p.rank;
CPU_frequency = p.CPU_frequency;
CPU_voltage = p.CPU_voltage;
};
CPU_Rank outRank() { return rank; };
int outF() { return CPU_frequency; };
double outV() { return CPU_voltage; };
};
class RAM {
private:
string name;
int RAM_frequency;
int RAM_capacity;
public:
RAM(string Name, int a, int b)
{
name = Name;
RAM_frequency = a;
RAM_capacity = b;
};
RAM()
{
name = '未知';
RAM_frequency = 0;
RAM_capacity = 0;
}
RAM(int a)
{
RAM_capacity = a;
}
~RAM()
{
cout << 'Good bye!' << endl;
}
string Pname() { return name; };
int P_ram_frequency() { return RAM_frequency; };
int P_ram_capacity() { return RAM_capacity; };
};
class CDROM {
private:
string Cname;
int speed;
public:
CDROM(string CName, int Cspeed)
{
Cname = CName;
speed = Cspeed;
};
CDROM()
{
Cname = '未知';
speed = 0;
};
CDROM(string M)
{
Cname = M;
}
~CDROM()
{
cout << 'Good bye!' << endl;
}
string P_Cname() { return Cname; };
int Pspeed() { return speed; };
};
class Computer :public CPU,public RAM,public CDROM{
public:
Computer(int a, int b, int c, string* N, int d, int e, string* M, int f) :CPU(a, b, c), RAM(*N, d, e), CDROM(*M, f) {};
Computer(int b, int a, string* N) :RAM(a), CPU(b), CDROM(*N) {};
void run() { cout << 'Computer开始运行!' << endl; }
void stop() { cout << 'Computer停止运行!' << endl; };
void output1()
{
cout << '我是组装机' << endl;
cout << 'CPU:' << endl;
cout << '等级:' << outRank() << '\n' << '频率:' << outF() << '\n' << '电压:' << outV() << endl;
cout << 'RAM:' << endl;
cout << '品牌:' << Pname() << '\n' << '频率:' << P_ram_frequency() << '\n' << '容量:' << P_ram_capacity() << endl;
cout << 'CDROM:' << endl;
cout << '品牌:' << P_Cname() << '\n' << '传输速率:' << Pspeed() << endl;
};
};
int main()
{
cout << '请按顺序输入一台组装机芯片(等级、频率、电压)、内存(品牌、主频、容量)、光驱参数(品牌、传输速率):' << endl;
int newrank, newfrequency, newvoltage;
cin >> newrank >> newfrequency >> newvoltage;
string Name;
int RAM_frequency,RAM_capacity;
cin >> Name >> RAM_frequency >> RAM_capacity;
string CName;
int Cspeed;
cin >> CName >> Cspeed;
Computer A (newrank,newfrequency,newvoltage,&Name,RAM_frequency,RAM_capacity,&CName,Cspeed);
A.run();
A.output1();
A.stop();
cout << '请按顺序输入一台品牌机内存大小、cpu的主频、光驱品牌:' << endl;
int a, b;
string Cname2;
cin >> a >> b >> Cname2;
Computer B(b,a,Cname2);
return 0;
}
错误在于构造函数重载时参数类型不一致,导致编译器无法确定使用哪一个构造函数。具体来说,CPU 类中有三个构造函数,分别是 CPU(int newRank, int newFrequency, int newVoltage),CPU() 和 CPU(int b),其中 CPU(int b) 的参数类型是 int,而在 Computer 类中的调用中使用了 CPU(b) 这个语句,编译器无法确定应该调用哪一个构造函数。
因此,需要将 CPU(int b) 改为 CPU(int newFrequency) 或者给 CPU(int b) 的参数类型加上默认值,例如 CPU(int b=0)。
修改后的代码:
#pragma once
#include<iostream>
#include<string.h>
using namespace std;
enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
class CPU {
private:
CPU_Rank rank;
int CPU_frequency;
double CPU_voltage;
public:
CPU(int newRank, int newFrequency, int newVoltage)
{
rank = (CPU_Rank)newRank;
CPU_frequency = newFrequency;
CPU_voltage = newVoltage;
};
CPU()//构造函数重载
{
rank = P6;
CPU_frequency = 3000;
CPU_voltage = 220;
}
CPU(int newFrequency) // 将参数类型改为 int newFrequency
{
CPU_frequency = newFrequency;
}
~CPU()
{
cout << 'Good bye!' << endl;
}
CPU(const CPU& p)
{
rank = p.rank;
CPU_frequency = p.CPU_frequency;
CPU_voltage = p.CPU_voltage;
};
CPU_Rank outRank() { return rank; };
int outF() { return CPU_frequency; };
double outV() { return CPU_voltage; };
};
class RAM {
private:
string name;
int RAM_frequency;
int RAM_capacity;
public:
RAM(string Name, int a, int b)
{
name = Name;
RAM_frequency = a;
RAM_capacity = b;
};
RAM()
{
name = '未知';
RAM_frequency = 0;
RAM_capacity = 0;
}
RAM(int a)
{
RAM_capacity = a;
}
~RAM()
{
cout << 'Good bye!' << endl;
}
string Pname() { return name; };
int P_ram_frequency() { return RAM_frequency; };
int P_ram_capacity() { return RAM_capacity; };
};
class CDROM {
private:
string Cname;
int speed;
public:
CDROM(string CName, int Cspeed)
{
Cname = CName;
speed = Cspeed;
};
CDROM()
{
Cname = '未知';
speed = 0;
};
CDROM(string M)
{
Cname = M;
}
~CDROM()
{
cout << 'Good bye!' << endl;
}
string P_Cname() { return Cname; };
int Pspeed() { return speed; };
};
class Computer :public CPU,public RAM,public CDROM{
public:
Computer(int a, int b, int c, string* N, int d, int e, string* M, int f) :CPU(a, b, c), RAM(*N, d, e), CDROM(*M, f) {};
Computer(int b, int a, string* N) :RAM(a), CPU(b), CDROM(*N) {};
void run() { cout << 'Computer开始运行!' << endl; }
void stop() { cout << 'Computer停止运行!' << endl; };
void output1()
{
cout << '我是组装机' << endl;
cout << 'CPU:' << endl;
cout << '等级:' << outRank() << '\n' << '频率:' << outF() << '\n' << '电压:' << outV() << endl;
cout << 'RAM:' << endl;
cout << '品牌:' << Pname() << '\n' << '频率:' << P_ram_frequency() << '\n' << '容量:' << P_ram_capacity() << endl;
cout << 'CDROM:' << endl;
cout << '品牌:' << P_Cname() << '\n' << '传输速率:' << Pspeed() << endl;
};
};
int main()
{
cout << '请按顺序输入一台组装机芯片(等级、频率、电压)、内存(品牌、主频、容量)、光驱参数(品牌、传输速率):' << endl;
int newrank, newfrequency, newvoltage;
cin >> newrank >> newfrequency >> newvoltage;
string Name;
int RAM_frequency,RAM_capacity;
cin >> Name >> RAM_frequency >> RAM_capacity;
string CName;
int Cspeed;
cin >> CName >> Cspeed;
Computer A (newrank,newfrequency,newvoltage,&Name,RAM_frequency,RAM_capacity,&CName,Cspeed);
A.run();
A.output1();
A.stop();
cout << '请按顺序输入一台品牌机内存大小、cpu的主频、光驱品牌:' << endl;
int a, b;
string Cname2;
cin >> a >> b >> Cname2;
Computer B(b,a,Cname2);
return 0;
}
通过修改,编译器就可以正确识别并调用 CPU 类中的构造函数了。
注意:
- 在构造函数重载时,参数类型必须有所不同,才能使编译器能够正确识别调用哪个构造函数。
- 如果多个构造函数的参数类型相同,但参数顺序不同,那么编译器也无法确定使用哪一个构造函数。
- 在实际编程中,尽量避免构造函数参数类型相同的情况,以提高代码的可读性和可维护性。
原文地址: https://www.cveoy.top/t/topic/nnhZ 著作权归作者所有。请勿转载和采集!