方法不是类的静态成员

时间:2012-10-30 08:43:43

标签: c++ class header

大家好我在编译时遇到了这个错误

error:'unisgned int vehicle::accelerate' is not a static member of 'class vehicle'

知道如何解决这个问题吗?

标头文件

class vehicle
{
    public:
    enum Switch
    {
     SWITCH_ON=0,
     SWITCH_OFF
                };
     vehicle();
    ~vehicle();

    bool powerSwitch(Switch );
    unsigned int accelerate(unsigned int );
    unsigned int decelerate(unsigned int );
    bool isMoving();
    unsigned int getSpeed();
    unsigned int setSpeed(unsigned int);

private:
unsigned int speed;
bool moving;
};

vehicle.cpp

unsigned int vehicle::accelerate(amount)
{

if(moving==true;){
speed+=amount;
 }

 return speed;

 }

3 个答案:

答案 0 :(得分:4)

您缺少参数列表中的类型:

unsigned int vehicle::accelerate(unsigned int amount)
{
  .....
}

答案 1 :(得分:2)

正如你所说:

unsigned int accelerate(unsigned int );

所以你必须实现:

unsigned int vehicle::accelerate(unsigned int amount)
{
  //...

此时需要再次给出类型。

答案 2 :(得分:1)

该错误可能位于其他地方,您尝试访问的accelerate成员不使用运营商->.,而是::,除了忘记参数的类型