【发布时间】:2016-03-24 01:28:35
【问题描述】:
我觉得我在兜圈子,试图查明为什么会发生编译器错误。我将给出我得到的编译器错误的(长)列表,并希望有人可以就如何纠正这些错误提出建议。我包括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 类的 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);
}
}
}
}
}
【问题讨论】:
-
一方面,World.cpp 应该
#include world.h。这可能会一次解决你的很多问题。通常,当您遇到很多错误时,可能会通过修复一两个基本问题来解决。 -
#include "world.h"在实施中 -
等等,你是否一次编写了所有这些代码,没有尝试编译任何代码,只是现在你试图编译所有这些代码?那是完全错误的做法。正确的做法是:写一点代码,编译它,测试它,确保它可以工作,然后再写一些代码,然后重复。
-
请查看minimal reproducible example 是什么。这就是我们正在寻找的问题形式。它还可以帮助您找出问题所在。继续删除东西,直到找到仍然可以重现错误的最小单元......然后看看你是否能从那个小单元中找出错误是什么。
-
@TingRay 这不是按需做作业的服务。