当我尝试使用构造函数创建 pc 对象时,在第 65 行出现错误。
错误:
1) E0289 отсутствуют экземпляры конструктора "computer::computer", соответствующие списку аргументов
2) C2664 "computer::computer(computer &&)": невозможно преобразовать аргумент 1 из "const char [5]" в "char *"
我正确地传递了所有参数。但是还是有问题。如何解决这个问题?
编码:
#include <iostream>
using namespace std;
class screen {
public:
screen(char* t, long c, int x, int y) {
strcpy(type, t);
colors = c;
x_resolution = x;
y_resolution = y;
}
void show_screen(void);
private:
char type[30];
long colors;
int x_resolution;
int y_resolution;
};
void screen::show_screen(void) {
cout << "Тип экрана: " << type << endl;
cout << "Количество цветов: " << colors << endl;
cout << "Распределительная способность: " << x_resolution << " x " << y_resolution << " y " << endl;
}
class mother_board {
public:
mother_board(int p, int s, int r) {
processor = p;
speed = s;
RAM = r;
}
void show_mother_board();
private:
int processor;
int speed;
int RAM;
};
void mother_board::show_mother_board(void) {
cout << "Процессор: " << processor << endl;
cout << "Частота: " << speed << endl;
cout << "ОЗУ: " << RAM << " Мб" << endl;
}
class computer : public screen, public mother_board {
public:
computer(char* n, int h, float f, char* s, long c, int x, int y, int p, int sp, int r) : screen(s, c, x, y), mother_board(p, sp, r) {
strcpy(name, n);
hard_disc = h;
floppy = f;
}
void show_computer(void);
private:
char name[50];
int hard_disc;
float floppy;
};
void computer::show_computer(void) {
cout << "Тип компьютера: " << name << endl;
cout << "Жесткий диск: " << hard_disc << " Мб" << endl;
cout << "Дискета: " << floppy << " Мб" << endl;
show_mother_board();
show_screen();
}
int main()
{
computer pc("Sony", 1024, 1.44, "SVGA", 64000000, 780, 1024, 686, 66, 1024);
pc.show_computer();
return 0;
}
任何类型的文字
"Sony"都是 const char* 类型(或者更确切地说,请参阅下面的评论KoVadim)。在参数中,添加const к char*.如果我们不谈论最好不要开设此类课程的事实...