I want to make an argument for one of the member functions optional. When no argument is provided, it would use an member variable.
However, when I tried to compile it it shows "error: invalid use of non-static data member 'Object::initPos'"
Just to isolate the problem, I tried defaulting an int type and it compiled fine.
I wonder what is the problem with my code and how I could use a member function as default value.
Thank you for your help!
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;
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…