枚举即使包括在内也不被认可

时间:2013-07-18 23:38:20

标签: c++ enums compiler-errors syntax-error

我对C ++很陌生,并且有一个烦人的错误:这个以前功能正常的代码由于某种原因停止了工作。在编译时,我得到的第一个错误如下所示。我认为由于某种原因它不会识别枚举类型Material,即使它是导入的。

1>...\chunk.h(10): error C2146: syntax error : missing ';' before identifier 'terrain'
1>...\chunk.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\chunk.h(10): error C2065: 'chunkWidth' : undeclared identifier
1>...\chunk.h(10): error C2065: 'chunkWidth' : undeclared identifier
1>...\chunk.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\world.h(14): error C2146: syntax error : missing ';' before identifier 'get'
1>...\world.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\world.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\world.h(14): warning C4183: 'get': missing return type; assumed to be a member function returning 'int'
1>...\world.h(6): error C2011: 'World' : 'class' type redefinition
1>          ...\world.h(6) : see declaration of 'World'
1>...\world.cpp(9): error C2027: use of undefined type 'World'
1>          ...\world.h(6) : see declaration of 'World'

Chunk.h

#pragma once
#include "main.h"

class Chunk {
    private:
        int xpos;
        int ypos;
    public:
        Chunk(int xRelToSpawn, int yRelToSpawn);
        Material terrain[chunkWidth][chunkWidth];
        int getXpos();
        int getYpos();
};

main.h

#pragma once
#include "World.h"

//const int gridSizeX = 30;
//const int gridSizeY = 30;
const int chunkWidth = 15;
const int chunkHeight = 15;
extern int viewportX;
extern int viewportY;
const int viewportWidth = 15;
const int viewportHeight = 15;

enum Material{SAND, WATER};

extern World world = World();

World.h

#include <vector>
#include "main.h"
#include "Chunk.h"
using namespace std;

class World {
    private:
        vector<Chunk> chunks;
    public:
        World();
        ~World();
        bool chunkExists(int x, int y);
        //returns material for absolute coordinates
        Material get(int x, int y);
        //returns world coordinates for given chunk coordinates
        int* getAbsCoords(int chunkIndex, int x, int y);
        int* getChunkCoords(int x, int y);
        Chunk getChunk(int index);
        int getChunkIndex(int x, int y);
        int chunkIndexAbove(int chunkIndex);
        int chunkIndexBelow(int chunkIndex);
        int chunkIndexLeft(int chunkIndex);
        int chunkIndexRight(int chunkIndex);
};

感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

你有的地方

#include "chunk.h"

所以处理器停止该文件并开始读取chunk.h:

#pragma once
#include "main.h"

所以处理器开始读取main.h:

#pragma once
#include "World.h"

所以处理器开始读取World.h:

#include <vector>
#include "main.h" //main.h was pragma'd so this is ignored
#include "Chunk.h" //chunk.h was pragma'd so this is ignored
using namespace std;

class World {
    private:
        vector<Chunk> chunks; //here, the compiler should be confused
                              //it hasn't seen what a "Chunk" is yet.

您有循环依赖项。解决这个问题的方法在理论上很简单,但在实践中却很棘手。第一:将所有类型/全局/函数放在一个顺序中:

Material //material needs nothing else to be defined
Chunk //chunk needs material, nothing else to be defined
World //world needs Chunk _and_ material to be defined
extern World world = World(); //needs World to be defined

然后编辑标题,以便数据按此顺序排列。也就是说,包含Material的标头不应包含包含Chunk,World或world的标头。并且包含Chunk的标头不应包含包含Worldworld的标头。

在您的情况下,类型没有循环依赖关系,因此这相对容易。将Material移动到块头(或它自己的头),以便块头不需要包含,并且不需要包含主头。

Chunk.h //contains Material and Chunk
World.h //contains World
Main.h  //contains extern World world

<小时/> 我不认为extern World world = World()在标题中有效。我想你必须删除= World(),然后在cpp文件中,把这行:

World world;

这是实际的全局变量。标题中的extern仅允许所有其他文件知道此变量存在于某处。

答案 1 :(得分:1)

避免循环包含的标准做法是:

  • 如果您使用的是Microsoft编译器,请在文件开头添加#pragma once
  • 要在.h文件的开头使用任何编译器:

    #ifndef YOURFILENAME_H

    #define YOURFILENAME_H

    #include&#34; World.h&#34;

    ... .h文件的正文 ...

    #endif // ndef YOURFILENAME_H