简体   繁体   English

即使包含枚举也无法识别

[英]Enum not recognized even though it is included

I am fairly new to C++, and am having an annoying bug: this previously functional code has stopped working for some reason. 我对C ++相当陌生,并且遇到了一个令人讨厌的错误:由于某些原因,此先前起作用的代码已停止工作。 Upon compilation the first errors I get are shown below. 编译后,我得到的第一个错误如下所示。 I think that for some reason it isn't recognizing the enum type Material , even though it is imported. 我认为由于某种原因,即使它是导入的,也无法识别枚举类型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);
};

Any help is appreciated. 任何帮助表示赞赏。

Somewhere you have 你有某个地方

#include "chunk.h"

So the processor stops that file and starts reading chunk.h instead: 因此,处理器将停止该文件并开始读取chunk.h:

#pragma once
#include "main.h"

So the processor starts reading main.h instead: 因此,处理器开始改为读取main.h:

#pragma once
#include "World.h"

So the processor starts reading World.h instead: 因此,处理器开始改为读取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.

You have circular dependencies. 您有循环依赖项。 The way to fix this is simple in theory, but tricky in practice. 解决此问题的方法理论上很简单,但实际上却很棘手。 First: Put all of your types/globals/functions in an order: 第一:将所有类型/全局变量/函数按顺序排列:

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

Then edit your headers so that the data is in this order. 然后编辑标题,以使数据按此顺序排列。 Namely, the header containing Material should NOT include the header containing Chunk, World, or world . 即,包含Material的标头不应包含包含Chunk,World或world的标头。 And the header containing Chunk should NOT include the header containing World or world . 并且包含Chunk的标题不应包含包含Worldworld的标题。

In your case, the types don't have circular dependencies, so this is relatively easy. 在您的情况下, 类型没有循环依赖关系,因此这相对容易。 Move Material to the chunk header (or it's own header) so that the chunk header needs no includes, and nothing needs to include the main header. Material移到块头(或它自己的头),以便块头不需要包含,也不需要包含主头。

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


I don't think that extern World world = World() works in a header. 我不认为extern World world = World()在标题中起作用。 I think you'll have to remove the = World() , and then in a cpp file, put the line: 我认为您必须删除= World() ,然后在cpp文件中放入以下行:

 World world; 

This is the actual global variable. 这是实际的全局变量。 The extern in the header merely lets all the other files know that this variable exists somewhere. 标头中的extern仅让所有其他文件知道此变量存在于某处。

Standard practice for avoiding circular includes is: 避免循环包括的标准做法是:

  • If you're using a Microsoft compiler, add #pragma once at the start of the file. 如果您使用的是Microsoft编译器,请在文件开头添加#pragma once
  • To work with any compiler, at the start of the .h file: 要与任何编译器一起使用,请在.h文件的开头:

    #ifndef YOURFILENAME_H #ifndef YOURFILENAME_H

    #define YOURFILENAME_H #define YOURFILENAME_H

    #include "World.h" #include“ World.h”

    ... body of the .h file ... .... h文件的主体...

    #endif //ndef YOURFILENAME_H #endif // ndef YOURFILENAME_H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM