返回临时对象在头文件:无法从功能
class Point{
public:
Point(); // constructor
Point(double x, double y); // constructor
Point(Point& A); //Copy Constructor
~Point(); // destructor
// below are the operator declarations.
Point operator -() const; // Negate the coordinates.
private:
double xCord;
double yCord;
};
在CPP的执行文件,相关的构造函数:
Point::Point(double x, double y){ // constructor
X(x);// void X(double x) is a function to set the xCord
Y(y);// so is Y(y)
}
Point Point::operator-() const{ // Negate the coordinates.
Point temp(-xCord,-yCord);
return temp;
// return Point(-xCord,-yCord); // cannot use this one
}
看来,我不能把一个构造函数中返回线路。这是确定建立一个代码,但如果我把它放在回报,它会提供以下错误:
error: no matching function for call to ‘Point::Point(Point)’
,然后编译器会列出所有我的建设者。但是,嘿,它显然需要两个双重的论点,而不是一个Point类。那为什么呢?
而且我注意到,在由教授给出的示例代码,他们都很好:
Complex::Complex(double dx, double dy)
{
x = dx;
y = dy;
}
Complex Complex::operator -() const
{
return Complex(- x, - y);
}
您的副本构造函数不允许临时文件被复制。 – chris
'Point(Point & A); //复制构造函数不是一个有用的复制构造函数,你需要'Point(const Point & A);''因为你不能从临时实例中复制 –
'Point :: Point(double x,double y){//构造函数 X(x); Y(y);'使用初始化列表 – Manu343726