【问题标题】:Method not a static member of a class方法不是类的静态成员
【发布时间】:2012-10-19 13:53:40
【问题描述】:

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

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;
};

车辆.cpp

unsigned int vehicle::accelerate(amount)
{

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

 return speed;

 }

【问题讨论】:

  • 定义应该是unsigned int vehicle::accelerate(unsigned int amount)
  • 你的 if 语句应该省略分号。

标签: c++ class header


【解决方案1】:

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

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

【讨论】:

  • +1 对于一个虚假的参数来说,这是一个多么奇怪的错误。编译器上的 K&R-panic-mode? =P
  • @WhozCraig,原来的代码确实有这个:vehicle::accelerate(amount)
  • @chris 是的,我看到了,尽管他报告的错误对于函数 def 的实际错误是完全奇怪的。我认为错误(静态成员 err)是不相关的,并且由于我们没有匹配的行号,我们将永远不知道它来自哪里
  • @Nobody 啊。那是有道理的。两个错误(破坏定义,无效调用)。谢谢。一分钟我真的很头疼。
【解决方案2】:

正如你所说:

unsigned int accelerate(unsigned int );

所以你必须实现:

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

此时需要再次给出类型。

【讨论】:

    【解决方案3】:

    错误可能出在其他地方,您尝试访问 accelerate 成员时不使用运算符 ->.,而是使用 ::,除了忘记参数的类型

    【讨论】:

    • int main(int argc, char *argv[]) { 车辆 t; t.setSpeed(5); cout
    • 这就是我调用方法的方式
    • @user1750901,您在isMoving() 上缺少括号。
    • @user1750901 将此代码编辑到您的问题中,并请标记与错误消息相关的行。
    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2019-01-10
    相关资源
    最近更新 更多