我对C++很陌生,而且我的类定义有问题。这很可能是一个非常基本的操作,但我无法在网上找到任何相关资源,因为我不太确定要搜索什么。C++ - 从.h文件获取.cpp文件中的类变量
标题说明了一切。我有一个如下所示的头文件:
class Rectangle{
public:
int Width();
int Height();
int Area();
void Resize();
private:
int width;
int height;
}
然后我有下面的.cpp文件。
int Rectangle::Width()
{
//return the width
}
int Rectangle::Height()
{
//return the height
}
int Rectangle::Area()
{
//return the area
}
void Rectangle::Resize()
{
//resize the rectangle
}
正如你所看到的,我已经评论说,我希望做的操作,但我不太清楚如何从头文件访问的变量int width
和int height
。
同样,这可能看起来像一个简单的操作,但我不知道如何解决这个问题。任何帮助,高度赞赏!
您是否在cpp文件中包含头文件? –
只是单挑:你在课堂定义之后缺少';'。 – MicroVirus
您需要包含头文件,并确保在类定义的最后一个大括号后面有分号。 – dasblinkenlight