为什么这个c ++代码不能编译?

时间:2021-04-21 16:58:17

Seeig that i'm new to C++ I thought i'd try and write a very simple console app that populates a 2D-array and displays its contents.

Seeig我是C ++的新手我以为我会尝试编写一个非常简单的控制台应用程序来填充2D数组并显示其内容。

But the code I've written won't compile.

但是我写的代码不会编译。

Some of the errors I get are:

我得到的一些错误是:

error C2065: 'box' : undeclared identifier
error C2228: left of '.GenerateBox' must have class/struct/union

错误C2065:'box':未声明的标识符错误C2228:'.GenerateBox'的左边必须有class / struct / union

Here is my code:

这是我的代码:

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

Any idea whats causing these errors?

有什么想法导致这些错误吗?

5 个解决方案

#1


8  

The C++ compiler reads source files in a single pass, from top to bottom. You have described the Box class at the bottom, after main(), after the part where you attempt to use the class. Accordingly, when the compiler gets to the part where you say 'Box box;', it has not yet seen the class definition, and thus has no idea what 'Box' means.

C ++编译器从上到下一次读取源文件。您已经在main()之后的底部描述了Box类,在您尝试使用该类的部分之后。因此,当编译器到达你说'Box box;'的部分时,它还没有看到类定义,因此不知道'Box'是什么意思。

#2


6  

Move the main function to the bottom of your code. Specifically, you need to define Box before you reference it.

将main函数移动到代码的底部。具体来说,您需要在引用之前定义Box。

The only time when you can get away with only a forward declaration (i.e. class Box;) is when you just use Box as a pointer or reference.

唯一一次只能使用前向声明(即类Box;)就可以使用Box作为指针或引用。

#3


5  

You have to define Box before using it. So for your small test you can put your class definition before the main.

您必须在使用之前定义Box。因此,对于您的小测试,您可以将您的类定义放在main之前。

For bigger programs, you will put your class definitions inside .h header files that you will include at the top of your source files.

对于较大的程序,您将把类定义放在源文件顶部的.h头文件中。

#4


2  

Is due to pre declaration of main(). Use main after declaration of class Box.

是由于预先声明main()。在使用类Box后声明。

#5


0  

@nikko is right. You have to declare the Box class before using it.By either

@nikko是对的。你必须在使用之前声明Box类。或者

  • cutting pasting the declaration
  • 切断宣言

  • or telling the compiler you will declare them later
  • 或告诉编译器您将在以后声明它们


try this

extern class Box;
//use box class here
//then define it later as you wish

#1


8  

The C++ compiler reads source files in a single pass, from top to bottom. You have described the Box class at the bottom, after main(), after the part where you attempt to use the class. Accordingly, when the compiler gets to the part where you say 'Box box;', it has not yet seen the class definition, and thus has no idea what 'Box' means.

C ++编译器从上到下一次读取源文件。您已经在main()之后的底部描述了Box类,在您尝试使用该类的部分之后。因此,当编译器到达你说'Box box;'的部分时,它还没有看到类定义,因此不知道'Box'是什么意思。

#2


6  

Move the main function to the bottom of your code. Specifically, you need to define Box before you reference it.

将main函数移动到代码的底部。具体来说,您需要在引用之前定义Box。

The only time when you can get away with only a forward declaration (i.e. class Box;) is when you just use Box as a pointer or reference.

唯一一次只能使用前向声明(即类Box;)就可以使用Box作为指针或引用。

#3


5  

You have to define Box before using it. So for your small test you can put your class definition before the main.

您必须在使用之前定义Box。因此,对于您的小测试,您可以将您的类定义放在main之前。

For bigger programs, you will put your class definitions inside .h header files that you will include at the top of your source files.

对于较大的程序,您将把类定义放在源文件顶部的.h头文件中。

#4


2  

Is due to pre declaration of main(). Use main after declaration of class Box.

是由于预先声明main()。在使用类Box后声明。

#5


0  

@nikko is right. You have to declare the Box class before using it.By either

@nikko是对的。你必须在使用之前声明Box类。或者

  • cutting pasting the declaration
  • 切断宣言

  • or telling the compiler you will declare them later
  • 或告诉编译器您将在以后声明它们


try this

extern class Box;
//use box class here
//then define it later as you wish