我想为其中一个成员函数可选参数。当没有提供参数时,它将使用成员变量。如何在C++中使用成员变量作为默认参数?
然而,当我试图编译它,它显示“错误:无效使用非静态数据成员的对象:: initPos“的”
只是为了隔离问题,我试着违约int类型它编译得很好。 我想知道我的代码有什么问题,以及如何使用成员函数作为默认值。
谢谢你的帮助!
Object.h
class Object
{
public:
...
void MoveTo(double speed, Point position);
protected:
Point initPos;
Point currPos;
};
Object.c
void Object::MoveTo(double speed, Point position = initPos)
{
currPos = postion;
}
Point.h
class Point
{
...
private:
double x;
double y;
double z;
};
你的问题让我这个产卵一个:http://stackoverflow.com/questions/9286801/valid-expressions-for-default-function-arguments – 2012-02-15 01:42:56