Seeig我是C++的新手我以为我会尝试编写一个非常简单的控制台应用程序来填充2D数组并显示其内容.
但是我写的代码不会编译.
我得到的一些错误是:
错误C2065:'box':未声明的标识符
错误C2228:'.GenerateBox'的左边必须有class/struct/union
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
Box box;
box.GenerateBox();
}
class Box
{
private:
static int const maxWidth = 135;
static int const maxHeight = 60;
char arrTest[maxWidth][maxHeight];
public:
void GenerateBox()
{
for (int i=0; i<maxHeight; i++)
for (int k=0; k<maxWidth; k++)
{
arrTest[i][k] = 'x';
}
for (int i=0; i<maxHeight; i++)
{
for (int k=0; k<maxWidth; k++)
{
cout << arrTest[i][k];
}
cout << "\n";
}
}
};
Run Code Online (Sandbox Code Playgroud)
有什么想法导致这些错误吗?
C++编译器从上到下一次读取源文件.您已经在main()后面的底部描述了Box类,在您尝试使用该类的部分之后.因此,当编译器到达你说'Box box;'的部分时,它还没有看到类定义,因此不知道'Box'是什么意思.
将main函数移动到代码的底部.具体来说,您需要Box在引用之前进行定义.
唯一一次只能使用前向声明(即class Box;)时,您只需将其Box用作指针或引用.
您必须在使用之前定义Box.因此,对于您的小测试,您可以将您的类定义放在main之前.
对于更大的程序,您将把类定义放在源文件顶部的.h头文件中.
| 归档时间: |
|
| 查看次数: |
299 次 |
| 最近记录: |