class Carriage{ public : Carriage(); // default constructor Carriage(Carriage &other); // copy constructor string getLogs(); void addLogEntry(int); void copy(Carriage &other); // copy method ~Carriage(); // des

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/06 02:28:05

class Carriage{ public : Carriage(); // default constructor Carriage(Carriage &other); // copy constructor string getLogs(); void addLogEntry(int); void copy(Carriage &other); // copy method ~Carriage(); // des
class Carriage{
public :
Carriage(); // default constructor
Carriage(Carriage &other); // copy constructor
string getLogs();
void addLogEntry(int);
void copy(Carriage &other); // copy method
~Carriage(); // destructor
Carriage& operator=(Carriage& other); // overloading of '='

private:
int* log;
int used;
int capacity;
};
Carriage::Carriage& operator=(const Carriage& other)
{
if(&other != this)
{
capacity=other.capacity;
delete [] log;
log = new int[capacity];
copy(other);
used = other.used;
}
return *this;
}
报错‘Carriage& operator=(const Carriage&)’ must be a nonstatic member function

class Carriage{ public : Carriage(); // default constructor Carriage(Carriage &other); // copy constructor string getLogs(); void addLogEntry(int); void copy(Carriage &other); // copy method ~Carriage(); // des
在类外定义成员函数时,要把返回类型写在前面,再写类名::,再写函数声明.而且参数类型要和声明完全一致,比如你的这个const就要去掉.
Carriage& Carriage::operator=(Carriage& other)
{
if(&other != this)
{
\x05capacity=other.capacity;
\x05delete [] log;
\x05log = new int[capacity];\x05
\x05copy(other);
\x05used = other.used;
}
return *this;
}