Fra*_*ina 1 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'
Run Code Online (Sandbox Code Playgroud)
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();
};
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
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);
};
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
你有的地方
#include "chunk.h"
Run Code Online (Sandbox Code Playgroud)
所以处理器停止该文件并开始读取chunk.h:
#pragma once
#include "main.h"
Run Code Online (Sandbox Code Playgroud)
所以处理器开始读取main.h:
#pragma once
#include "World.h"
Run Code Online (Sandbox Code Playgroud)
所以处理器开始读取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.
Run Code Online (Sandbox Code Playgroud)
你有循环依赖.解决这个问题的方法在理论上很简单,但在实践中却很棘手.第一步:将所有类型/全局/函数放在一个顺序中:
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
Run Code Online (Sandbox Code Playgroud)
然后编辑标题,以便数据按此顺序排列.也就是说,包含的标题Material不应包含包含Chunk,World或的标题world.并且包含的标题Chunk不应包含包含World或的标题world.
在您的情况下,类型没有循环依赖,因此这相对容易.移动Material到块头(或它自己的头),以便块头不需要包含,并且不需要包含主头.
Chunk.h //contains Material and Chunk
World.h //contains World
Main.h //contains extern World world
Run Code Online (Sandbox Code Playgroud)
extern World world = World()用于标题.我认为你必须删除= World(),然后在cpp文件中,放行:
World world;
Run Code Online (Sandbox Code Playgroud)
这是实际的全局变量.将extern在头只是让所有其他文件知道这个变量存在某处.