【问题标题】:Class Scope Error in C++ [CLOSED)C++ 中的类范围错误 [已关闭)
【发布时间】:2015-11-14 08:04:59
【问题描述】:

C++ 类中的范围错误。我有以下两个类:

class Tire
{
public:
    Tire();
    void width(int);
};

class Car
{
public:
    Car();
    void size();    
};

Car::Car()
{
    Tire mytire;
}
void Car::size()
{
    mytire.width(5);    // generates error: "mytire was not declared in this scope"
}

编辑: 愚蠢的错误。我带来的不便表示歉意。我只是没睡够。

【问题讨论】:

  • 我必须在两个不同的 IDE 中输入它,然后才能弄清楚错误是什么。
  • @JamesRoot 对由此给您带来的不便深表歉意。

标签: c++ scope


【解决方案1】:

mytire 不是成员,它是构造函数中的局部变量。
将其声明移到类定义中。

class Car
{
public:
    Car();
    ~Car();
    void size();
private:
    Tire mytire;
};

Car::Car()
{
}

【讨论】:

  • 哦,我真傻。我为这个问题道歉。我住的地方已经很晚了,我需要睡觉了。
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多