对象、枚举类型和范围的问题
Issues with objects, enumerated types, scope
我觉得自己在兜圈子,试图找出编译器错误发生的原因。我将给出我遇到的编译器错误的(长)列表,并希望有人能就如何纠正这些错误提出建议。我包括4个文件,其中一个很长,但为了完整性,我会提供整个文件。
如有任何小错误的帮助,我们将不胜感激。
编译器错误列表:
world.cpp:13:1: error: ‘World’ does not name a type
World::World(string explorer)
^
world.cpp:28:6: error: ‘World’ has not been declared
void World::setNumPits(int pits)
^
world.cpp: In function ‘void setNumPits(int)’:
world.cpp:30:9: error: ‘numPits’ was not declared in this scope
numPits = pits;
^
world.cpp: At global scope:
world.cpp:37:5: error: ‘World’ has not been declared
int World::getNumPits()
^
world.cpp: In function ‘int getNumPits()’:
world.cpp:39:9: error: ‘numPits’ was not declared in this scope
return numPits;
^
world.cpp: At global scope:
world.cpp:46:6: error: ‘World’ has not been declared
void World::setType()
^
world.cpp: In function ‘void setType()’:
world.cpp:50:2: error: ‘board’ was not declared in this scope
board[0][4].setType(type); // The explorer will always begin at this position
^
world.cpp:80:23: error: ‘numPits’ was not declared in this scope
for (int i = 0; i <= numPits; i++)
^
world.cpp: In function ‘void setFace(Facing)’:
world.cpp:108:7: error: ‘board’ was not declared in this scope
if (board[i][j].getType() == YOU)
^
world.cpp: In function ‘void setBreeze(bool)’:
world.cpp:132:25: error: ‘board’ was not declared in this scope
if (board[i + 1][j].getType() == PIT)
^
world.cpp:151:25: error: ‘board’ was not declared in this scope
if (board[i + 1][j].getType() == PIT)
^
world.cpp:171:25: error: ‘board’ was not declared in this scope
if (board[i][j + 1].getType() == PIT)
^
world.cpp:205:6: error: ‘World’ has not been declared
void World::setStench()
^
world.cpp:206:1: error: a function-definition is not allowed here before ‘{’ token
{
^
world.cpp:1600:1: error: expected ‘}’ at end of input
}
^
world.cpp:1600:1: error: expected ‘}’ at end of input
world.cpp: In function ‘int getNumPits()’:
world.cpp:40:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
gameobject.cpp:9:1: error: ‘GameObject’ does not name a type
GameObject::GameObject()
^
gameobject.cpp:19:6: error: ‘GameObject’ has not been declared
void GameObject::setFace(Facing direct)
^
gameobject.cpp:19:26: error: variable or field ‘setFace’ declared void
void GameObject::setFace(Facing direct)
^
gameobject.cpp:19:26: error: ‘Facing’ was not declared in this scope
gameobject.cpp:28:6: error: ‘GameObject’ has not been declared
void GameObject::setType(Type square)
^
gameobject.cpp:28:26: error: variable or field ‘setType’ declared void
void GameObject::setType(Type square)
^
gameobject.cpp:28:26: error: ‘Type’ was not declared in this scope
gameobject.cpp:38:1: error: ‘Facing’ does not name a type
Facing GameObject::getFace()
^
gameobject.cpp:47:1: error: ‘Type’ does not name a type
Type GameObject::getType()
^
gameobject.cpp:56:6: error: ‘GameObject’ has not been declared
void GameObject::setBreeze(bool pitAdj)
^
gameobject.cpp: In function ‘void setBreeze(bool)’:
gameobject.cpp:58:2: error: ‘breeze’ was not declared in this scope
breeze = pitAdj;
^
gameobject.cpp: At global scope:
gameobject.cpp:65:6: error: ‘GameObject’ has not been declared
void GameObject::setStench(bool wumpAdj)
^
gameobject.cpp: In function ‘void setStench(bool)’:
gameobject.cpp:67:2: error: ‘stench’ was not declared in this scope
stench = wumpAdj;
^
gameobject.cpp: At global scope:
gameobject.cpp:74:6: error: ‘GameObject’ has not been declared
bool GameObject::getBreeze()
^
gameobject.cpp: In function ‘bool getBreeze()’:
gameobject.cpp:76:9: error: ‘breeze’ was not declared in this scope
return breeze;
^
gameobject.cpp: At global scope:
gameobject.cpp:83:6: error: ‘GameObject’ has not been declared
bool GameObject::getStench()
^
gameobject.cpp: In function ‘bool getStench()’:
gameobject.cpp:85:9: error: ‘stench’ was not declared in this scope
return stench;
^
gameobject.cpp: In function ‘bool getBreeze()’:
gameobject.cpp:77:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
gameobject.cpp: In function ‘bool getStench()’:
gameobject.cpp:86:1: warning: control reaches end of non-void function [-Wreturn-type]
}
这是我的gameobject.h文件,它有我对gameobject的类定义
// Specification file for the GameObject class
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
using namespace std;
// GameObject class declaration
class GameObject
{
private:
Type type;
Facing face;
bool stench;
bool breeze;
public:
GameObject(); // Constructor
void setFace(Facing);
void setType(Type);
void setBreeze(bool);
void setStench(bool);
Facing getFace();
Type getType();
bool getBreeze();
bool getStench();
};
#endif
这是我的gameObject.cpp,
#include <iostream>
#include <string>
using namespace std;
//************************************************************
// Constructor for GameObject *
//************************************************************
GameObject::GameObject()
{
type = BLANK;
face = EAST;
}
//************************************************************
// setFace sets the value of the member variable face *
//************************************************************
void GameObject::setFace(Facing direct)
{
face = direct;
}
//************************************************************
// setType sets the value of the member variable type *
//************************************************************
void GameObject::setType(Type square)
{
type = square;
}
//************************************************************
// getFace sets the value of the member variable face *
//************************************************************
Facing GameObject::getFace()
{
return face;
}
//************************************************************
// getType sets the value of the member variable type *
//************************************************************
Type GameObject::getType()
{
return type;
}
//************************************************************
// setBreeze sets the value of the member variable breeze *
//************************************************************
void GameObject::setBreeze(bool pitAdj)
{
breeze = pitAdj;
}
//************************************************************
// setStench sets the value of the member variable stench *
//************************************************************
void GameObject::setStench(bool wumpAdj)
{
stench = wumpAdj;
}
//************************************************************
// getBreeze returns the value of the member variable breeze *
//************************************************************
bool GameObject::getBreeze()
{
return breeze;
}
//************************************************************
// getStench returns the value of the member variable stench *
//************************************************************
bool GameObject::getStench()
{
return stench;
}
这是我的world.h文件,它定义了世界级
//Specification file for the World Class
#ifndef WORLD_H
#define WORLD_H
#include "gameobject.h"
using namespace std;
enum Type { YOU, WUMPUS, GOLD, PIT, BLANK }; // Enumerated types of objects
enum Facing { NORTH, EAST, SOUTH, WEST }; // Enumerated directions
//World class declaration
class World
{
private:
GameObject board[4][4]; //Array of GameObjects
string name;
int numPits;
bool breeze;
bool stench;
Facing temp;
public:
World(string); //Constructor
void setNumPits(int);
int getNumPits();
void setType();
void setFace(Facing);
void setBreeze();
void setStench();
void setBoard();
void displayBoard();
void displayOptions(int);
void changeDirections();
void move();
void wumpusDeath();
void pitDeath();
void shoot();
void wumpusKilled();
void getGold();
void quitGame();
};
#endif
我不能包含我的整个world.cpp,但这是它的开始,我试图包含所有导致编译器错误的代码。
//Implementation file for World class
#include "gameobject.h" //For the GameObject class
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
//************************************************************
// Constructor for World *
//************************************************************
World::World(string explorer)
{
explorer = name;
// Get the system time
unsigned seed = time(0);
// Seed the random number generator
srand(seed);
}
//************************************************************
// setNumPits sets the vslue of the memeber variable numPits *
//************************************************************
void World::setNumPits(int pits)
{
numPits = pits;
}
//************************************************************
// getNumPits returns the value of the variable numPits *
//************************************************************
int World::getNumPits()
{
return numPits;
}
//************************************************************
// setType sets the member variable type in GameObject array *
//************************************************************
void World::setType()
{
Type type;
type = YOU;
board[0][4].setType(type); // The explorer will always begin at this position
// Randomly selects location of WUMPUS
int wRows, wCols;
wRows = rand() % 4;
wCols = rand() % 4;
// If square is takem, generates random values until a blank is found
while (board[wRows][wCols].getType() != BLANK)
{
wRows = rand() % 4;
wCols = rand() % 4;
}
type = WUMPUS;
board[wRows][wCols].setType(type); // Places the WUMPUS
// Randomly selects location of GOLD
int gRows, gCols;
gRows = rand() % 4;
gCols = rand() % 4;
// If square is taken, generates random values until a blank is found
while (board[gRows][gCols].getType() != BLANK)
{
gRows = rand() % 4;
gCols = rand() % 4;
}
type = GOLD;
board[gRows][gCols].setType(type); // Places the GOLD
// Randomly selects location of PITS
int pRows, pCols;
for (int i = 0; i <= numPits; i++) // Places the number of PITS randomly selected
{
pRows = rand() % 4;
pCols = rand() % 4;
// If square is taken, generates random values until a blank is found
while (board[pRows][pCols].getType() != BLANK)
{
pRows = rand() % 4;
pCols = rand() % 4;
}
type = PIT;
board[pRows][pCols].setType(type); // Places the PIT
}
// All other types remain BLANK
}
//************************************************************
// setFace sets the direction YOU is facing *
//************************************************************
void setFace(Facing direction)
{
Facing face;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (board[i][j].getType() == YOU)
{
face = direction;
board[i][j].setFace(face);
}
}
}
}
//************************************************************
// setBreeze sets the attribute breeze *
//************************************************************
void setBreeze(bool breeze)
{
bool breeze = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
// Tests corner squares for attributes
if (j == 4)
{
// Tests for breeze
if (board[i + 1][j].getType() == PIT)
{
breeze = true;
board[i + 1][j].getBreeze(breeze);
}
else if (board[i][j - 1].getType() == PIT)
{
breeze = true;
board[i][j - 1].getBreeze(breeze);
}
else if (board[i + 1][j - 1].getType() == PIT)
{
breeze = true;
board[i + 1][j - 1].getBreeze(breeze);
}
}
if (j == 0)
{
// Tests for breeze
if (board[i + 1][j].getType() == PIT)
{
breeze = true;
board[i + 1][j].getBreeze(breeze);
}
else if (board[i][j + 1].getType() == PIT)
{
breeze = true;
board[i][j + 1].getBreeze(breeze);
}
else if (board[i + 1][j + 1].getType() == PIT)
{
breeze = true;
board[i + 1][j + 1].getBreeze(breeze);
}
{
// Tests top row for attributes
if (j > 0 && j < 4)
{
// Tests for breeze
if (board[i][j + 1].getType() == PIT)
{
breeze = true;
board[i][j + 1].getBreeze(breeze);
}
else if (board[i][j - 1].getType() == PIT)
{
breeze = true;
board[i][j - 1].getBreeze(breeze);
}
else if (board[i + 1][j].getType() == PIT)
{
breeze = true;
board[i + 1][j].getBreeze(breeze);
}
else if (board[i + 1][j + 1].getType() == PIT)
{
breeze = true;
board[i + 1][j + 1].getBreeze(breeze);
}
else if (board[i + 1][j - 1].getType() == PIT)
{
breeze = true;
board[i + 1][j - 1].getBreeze(breeze);
}
}
}
}
}
//************************************************************
// setStench displays the game board *
//************************************************************
void World::setStench()
{
bool stench = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
// Tests corner squares for attributes
if (i == 0 && j == 4)
{
// Tests for breeze
if (board[i + 1][j].getType() == WUMPUS)
{
stench = true;
board[i + 1][j].getStench(stench);
}
else if (board[i][j - 1].getType() == WUMPUS)
{
stench = true;
board[i][j - 1].getStench(stench);
}
else if (board[i + 1][j - 1].getType() == WUMPUS)
{
stench = true;
board[i + 1][j - 1].getStench(stench);
}
}
if (i == 0 && j == 0)
{
// Tests for breeze
if (board[i + 1][j].getType() == WUMPUS)
{
stench = true;
board[i + 1][j].getStench(stench);
}
else if (board[i][j + 1].getType() == WUMPUS)
{
stench = true;
board[i][j + 1].getStench(stench);
}
else if (board[i + 1][j + 1].getType() == WUMPUS)
{
stench = true;
board[i + 1][j + 1].getStench(stench);
}
{
// Tests top row for attributes
if (i == 0 && j > 0 && j < 4)
{
// Tests for breeze
if (board[i][j + 1].getType() == WUMPUS)
{
stench = true;
board[i][j + 1].getStench(stench);
}
else if (board[i][j - 1].getType() == WUMPUS)
{
stench = true;
board[i][j - 1].getStench(stench);
}
else if (board[i + 1][j].getType() == WUMPUS)
{
stench = true;
board[i + 1][j].getStench(stench);
}
else if (board[i + 1][j + 1].getType() == WUMPUS)
{
stench = true;
board[i + 1][j + 1].getStench(stench);
}
else if (board[i + 1][j - 1].getType() == WUMPUS)
{
stench = true;
board[i + 1][j - 1].getStench(stench);
}
}
}
}
}
您得到的所有错误都是相关的:
'...' has not been declared
'...' does not name a type
'...' was not declared in this scope
它们意味着编译器找不到这些对象的声明,因为它们在单独的文件中,而您没有在需要的地方包含它们。
例如,在
gameObject.cpp
中,您声明了GameObject
类的函数,但没有包含gameobject.h
,因此编译器找不到该类,从而导致错误。
相关文章:
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- 命名空间范围问题
- 断言失败错误,C++中的矢量下标超出范围问题
- 指针范围问题和返回类中封装的指针向量内的指针引用
- PlatformIO 的范围问题(?)
- 无法编译 - 范围问题
- 需要帮助将矢量解决范围问题.C
- 来自 gmock 的默认值的范围问题
- 可能的继承范围问题:
- #pragma警告(disable:XXXX)的行为与预期不符(范围问题)
- OpenGL的范围问题
- C++ 引用变量范围问题
- 内存引用范围问题:变量值被重置,如何将值分配给LPCWSTR
- 矢量范围问题
- sed/regex - 更新具有范围问题的源文件
- C++类和范围问题
- C++ 程序未正确执行,可能是范围问题
- 类范围问题的向量
- 如何创建一个通用指针,它可以处理int和double变量并避免范围问题
- 命名空间别名范围问题