C++ перегрузка оператора сложения двух классов. Не находит походящий конструктор
// Содержание файла .h
class MemoryDevice {
private:
static int count;
string name;
unsigned int ID;
unsigned int MaxMemorySize;
unsigned int CurrentMemoryByte;
unsigned int FreeMemoryByte;
char* Memory;
public:
string getName();
unsigned int getMaxSize();
MemoryDevice();
MemoryDevice(std::string, unsigned int);
~MemoryDevice();
MemoryDevice operator + (MemoryDevice);
};
// Содержание файла .cpp
MemoryDevice::MemoryDevice(std::string name, unsigned int MaxMemory = 1024) {
this->name = name;
this->ID = count;
this->MaxMemorySize = MaxMemory;
this->FreeMemoryByte = MaxMemory;
this->Memory = new char[this->MaxMemorySize];
for (int i = 0; i < MaxMemory; i++)
{
Memory[i] = -1;
}
count++;
};
string MemoryDevice::getName() {
return this->name;
}
unsigned int MemoryDevice::getMaxSize() {
return this->MaxMemorySize;
};
MemoryDevice MemoryDevice::operator + (MemoryDevice c1) {
return MemoryDevice(c1.getName() + " " + this->getName(), (unsigned int)(c1.getMaxSize() + this->getMaxSize()));
};
Ответы (1 шт):
Автор решения: Alex
→ Ссылка
Создайте конструктор копирования MemoryDevice(MemoryDevice const& other), который будет левой стороной выражения.
И лучше в операторе + возвращать объект по ссылке, а не по значению. Так вы избавитесь от лишних вызовов конструктора/деструктора.
