Проблема с конструктором с параметрами
При попытке создать объект 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;
}
Ответы (1 шт):
Автор решения: AR Hovsepyan
→ Ссылка
Любой литерал типа "Sony" имеет тип const char*(вернее, смотрите комментарий ниже от KoVadim). В аргументах везде добавьте const к char*.
computer(const char* n, int h, float f, const char* s, ... )
Если не будем говорить о том, что таким классам лучше не быть...