我觉得我正在试图找出编译器错误发生的原因。我将提供我得到的(长)编译器错误列表,并希望有人可能会建议如何纠正这些错误。我包括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文件,它定义了World class
//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);
}
}
}
}
}
答案 0 :(得分:2)
您收到的所有错误都与之相关:
'...' has not been declared
'...' does not name a type
'...' was not declared in this scope
它们意味着编译器无法找到这些对象的声明,因为它们位于单独的文件中,并且您不需要将它们包含在内。
<小时/> 例如,在gameObject.cpp
中,您声明了GameObject
类的函数,但是您没有包含gameobject.h
,因此编译器找不到该类,这会导致错误。