我想为其中一个成员函数选择一个参数.如果没有提供参数,它将使用成员变量.
但是,当我尝试编译它时,它显示" 错误:无效使用非静态数据成员'Object :: initPos' "
只是为了隔离问题,我尝试默认一个int类型,它编译得很好.我想知道我的代码有什么问题,以及如何使用成员函数作为默认值.
谢谢您的帮助!
Object.h
class Object
{
public:
...
void MoveTo(double speed, Point position);
protected:
Point initPos;
Point currPos;
};
Run Code Online (Sandbox Code Playgroud)
Object.c
void Object::MoveTo(double speed, Point position = initPos)
{
currPos = postion;
}
Run Code Online (Sandbox Code Playgroud)
Point.h
class Point
{
...
private:
double x;
double y;
double z;
};
Run Code Online (Sandbox Code Playgroud)