我在C ++中遇到链接器错误。我一直试图弄清楚并试图使用gcc,clang ++和g ++(我正在使用它),但错误仍然没有消失。
这是我收到的错误消息:
Undefined symbols for architecture x86_64:
"Game::play()", referenced from:
_main in main-537fd9.o
"Game::Game(int, int, int)", referenced from:
_main in main-537fd9.o
"Game::~Game()", referenced from:
_main in main-537fd9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
有人可以帮帮我吗?谢谢!
main.cpp中:
#include "Game.h"
int main()
{
srand(static_cast<unsigned int>(time(0)));
// Create a game
// Use this instead to create a mini-game:
Game g(3, 3, 2);
Game g(9, 10, 40);
// Play the game
g.play();
}
Game.cpp:
#include "Game.h"
#include "globals.h"
#include "Pit.h"
#include "Player.h"
#include "Snake.h"
Game::Game(int rows, int cols, int nSnakes)
{
if (nSnakes < 0)
{
cout << "***** Cannot create Game with negative number of snakes!" << endl;
exit(1);
}
if (nSnakes > MAXSNAKES)
{
cout << "***** Cannot create Game with " << nSnakes
<< " snakes; only " << MAXSNAKES << " are allowed!" << endl;
exit(1);
}
if (rows == 1 && cols == 1 && nSnakes > 0)
{
cout << "***** Cannot create Game with nowhere to place the snakes!" << endl;
exit(1);
}
// Create pit
m_pit = new Pit(rows, cols);
// Add player
int rPlayer = 1 + rand() % rows;
int cPlayer = 1 + rand() % cols;
m_pit->addPlayer(rPlayer, cPlayer);
// Populate with snakes
while (nSnakes > 0)
{
int r = 1 + rand() % rows;
int c = 1 + rand() % cols;
// Don't put a snake where the player is
if (r == rPlayer && c == cPlayer)
continue;
m_pit->addSnake(r, c);
nSnakes--;
}
}
Game::~Game()
{
delete m_pit;
}
void Game::play()
{
Player* p = m_pit->player();
if (p == nullptr)
{
m_pit->display("");
return;
}
string msg = "";
while ( ! m_pit->player()->isDead() && m_pit->snakeCount() > 0)
{
m_pit->display(msg);
msg = "";
cout << endl;
cout << "Move (u/d/l/r//q): ";
string action;
getline(cin,action);
if (action.size() == 0)
p->stand();
else
{
switch (action[0])
{
default: // if bad move, nobody moves
cout << '\a' << endl; // beep
continue;
case 'q':
return;
case 'u':
case 'd':
case 'l':
case 'r':
p->move(decodeDirection(action[0]));
break;
}
}
m_pit->moveSnakes();
}
m_pit->display(msg); }