我對C++相當陌生,並且有一個惱人的bug:這個以前的函數代碼由於某種原因停止了工作。編譯後,我得到的第一個錯誤如下所示。我認爲,由於某些原因,即使它被導入,它仍不能識別枚舉類型Material
。Enum雖然被包含但未被識別
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);
};
任何幫助表示讚賞。
你確定你知道什麼是extern嗎? – Borgleader
我可以向你保證,我並不完全瞭解'extern'的功能或用途。 – FracturedRetina
我懷疑是World.h包含Chunk.h,所以你有循環包含。 – Puppy