1

我对 C++ 相当陌生,并且遇到了一个烦人的错误:这个以前的功能代码由于某种原因已经停止工作。编译后我得到的第一个错误如下所示。我认为由于某种原因它无法识别 enum type 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'

块.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();
};

主文件

#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();

世界.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);
};

任何帮助表示赞赏。

4

2 回答 2

6

你有的地方

#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的​​标头不应包含包含Worldor的标头world

在您的情况下,类型没有循环依赖关系,所以这相对容易。移动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头中的 仅让所有其他文件知道该变量存在于某处。

于 2013-07-19T17:54:04.673 回答
1

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

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

    #ifndef 你的文件名_H

    #define YOURFILENAME_H

    #include "世界.h"

    ... .h 文件的主体 ...

    #endif //ndef 你的文件名_H

于 2014-03-28T21:18:25.000 回答