如何解决这个问题?
只是得到以下错误:
g++ 输出:
duplicate symbol _game in:
/var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/main-62666f.o
/var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/Game-a5a31d.o
duplicate symbol _game in:
/var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/main-62666f.o
/var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/Write-83f8ee.o
看来问题不在头文件中。
编辑:头文件如下所示:
#ifndef GAME_H
#define GAME_H
#include "Color.h"
#include "Tile.h"
class Game
{
public:
Game();
~Game();
Color getActivePlayer();
void setStarttile(Tile Firststarttile);
Color togglePlayer();
void setRunning(bool run);
char newActiveplayer;
void run();
void runsecondmethod();
private:
Game(const Game &);
Color Activeplayer;
Tile *Starttile;
bool Running;
}game;
#endif
最佳答案
您在头文件“Game.h”中实例化了一个Game
实例,由于它被包含在多个文件中,您最终会得到多个game
实例在链接时。变化:
class Game
{
public:
...
}game;
到:
class Game
{
public:
...
};
extern Game game;
然后添加:
Game game;
在您的一个 .cpp 文件中。
关于c++ - ld : duplicate symbols for architecture x86_64 when defining a global var in a header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352884/