我想让一个成员函数的参数变为可选项。当没有提供参数时,它将使用一个成员变量。
然而,当我尝试编译它时,它显示:
error: invalid use of non-static data member 'Object::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;
};