(C++)与命名空间的链接会导致重复的符号错误

Gfd*_*ing 5 c++ linker namespace-organisation

在过去的几天里,我一直在试图找出如何链接我一直在努力的CLI游戏项目的文件.项目有两半,即客户端和服务器代码.

客户需要我制作的两个库.第一个是通用游戏板.这在GameEngine.h和GameEngine.cpp之间分开.头文件看起来像这样

namespace gfdGaming {

//  struct sqr_size {
//      Index x;
//      Index y;
//  };
    typedef struct { Index x, y; } sqr_size; 
    const sqr_size sPos = {1, 1};
    sqr_size sqr(Index x, Index y);
    sqr_size ePos;
    class board
    {
    // Prototypes / declarations for the class
    }
}
Run Code Online (Sandbox Code Playgroud)

CPP文件只是提供所有内容

#include "GameEngine.h"

type gfdGaming::board::functions
Run Code Online (Sandbox Code Playgroud)

客户端还将特定于游戏的代码(在本例中为TicTacToe)拆分为声明和定义(TTT.h,Client.cpp).TTT.h基本上是

#include "GameEngine.h"

#define TTTtar "localhost"
#define TTTport 2886

using namespace gfdGaming;
void* turnHandler(void*);
namespace nsTicTacToe 
{
    GFDCON gfd;
    const char X = 'X';
    const char O = 'O';
    string MPhostname, mySID;
    board TTTboard;
    bool PlayerIsX = true, isMyTurn;
    char Player = X, Player2 = O;

    int recon(string* datHolder = NULL, bool force = false);
    void initMP(bool create = false, string hn = TTTtar);
    void init();
    bool isTie();
    int turnPlayer(Index loc, char lSym = Player);
    bool checkWin(char sym = Player);

    int mainloop();

    int mainloopMP();

}; // NS
Run Code Online (Sandbox Code Playgroud)

我决定将它放在一个命名空间中来对它进行分组而不是一个类,因为有些部分在OOP中不能很好地工作,并且以后实现起来要容易得多.

我在过去连接客户端时遇到了麻烦,但这种设置似乎有效.

我的服务器也分为两个文件,Server.h和Server.cpp.

Server.h包含:

#include "../TicTacToe/TTT.h" // Server needs a full copy of TicTacToe code

class TTTserv;
struct TTTachievement_requirement {
    Index id;
    Index loc;
    bool inUse;
};
struct TTTachievement_t {
    Index id;
    bool achieved;
    bool AND, inSameGame;
    bool inUse;
    bool (*lHandler)(TTTserv*);
    char mustBeSym;
    int mustBePlayer;
    string name, description;
    TTTachievement_requirement steps[safearray(8*8)];

};

class achievement_core_t : public GfdOogleTech {
public: // May be shifted to private
    TTTachievement_t list[safearray(8*8)];
public:
    achievement_core_t();

    int insert(string name, string d, bool samegame, bool lAnd, int lSteps[8*8], int mbP=0, char mbS=0);
};


struct TTTplayer_t {
    Index id;
    bool inUse;
    string ip, sessionID;
    char sym;
    int desc;
    TTTachievement_t Ding[8*8];
};
struct TTTgame_t {
    TTTplayer_t Player[safearray(2)];
    TTTplayer_t Spectator;
    achievement_core_t achievement_core;
    Index cTurn, players;
    port_t roomLoc;
    bool inGame, Xused, Oused, newEvent;
};


class TTTserv : public gSserver {
    TTTgame_t Game;
    TTTplayer_t *cPlayer;

    port_t conPort;
public:
    achievement_core_t *achiev;
    thread threads[8];
    int parseit(string tDat, string tsIP);
    Index conCount;
    int parseit(string tDat, int tlUser, TTTplayer_t** retval);

private:
    int parseProto(string dat, string sIP);
    int parseProto(string dat, int lUser);

    int cycleTurn();
    void setup(port_t lPort = 0, bool complete = false);

public:
    int newEvent;
    TTTserv(port_t tlPort = TTTport, bool tcomplete = true);

    TTTplayer_t* userDC(Index id, Index force = false);
    int sendToPlayers(string dat, bool asMSG = false);
    int mainLoop(volatile bool *play);
};



// Other 
void* userHandler(void*);
void* handleUser(void*);
Run Code Online (Sandbox Code Playgroud)

在CPP文件中,我包含Server.h并提供main()和先前声明的所有函数的内容.

现在针对手头的问题 我在链接服务器时遇到问题.更具体地说,我在nsTicTacToe中的每个变量都有一个重复的符号错误(也可能在gfdGaming中).由于我需要TicTacToe函数,因此在构建服务器时链接Client.cpp(不带main())

ld: duplicate symbol nsTicTacToe::PlayerIsX       in Client.o and Server.o
collect2: ld returned 1 exit status
Command /Developer/usr/bin/g++-4.2 failed with exit code 1

It stops once a problem is encountered, but if PlayerIsX is removed / changed temporarily than another variable causes an error
Run Code Online (Sandbox Code Playgroud)

基本上,我正在寻找有关如何更好地组织我的代码以希望修复这些错误的任何建议.

免责声明: - 如果我提供的信息太多或太少,我会提前道歉,因为这是我第一次发帖

- 我尝试使用static和extern来解决这些问题,但显然这些并不是我需要的

感谢任何花时间阅读所有这些并做出回应的人=)

max*_*ost 6

您会收到有关重复定义的错误,因为这就是您所拥有的:每次.cpp文件包含TTT.h时,bool PlayerIsX都会定义一个全局(在nsTicTacToe命名空间中,但仍然是全局的).在这种情况下,包含它的是Server.cpp和Client.cpp.

解决此问题的一种方法是通过使用extern,然后在相应的.cpp文件(例如TTT.cpp)中执行实际定义,将定义更改为声明.

在TTT.h中:

namespace nsTicTacToe {
   ...
   extern bool PlayerIsX;
   ...
}
Run Code Online (Sandbox Code Playgroud)

在TTT.cpp中:

#include "TTT.h"

bool nsTicTacToe::PlayerIsX;
Run Code Online (Sandbox Code Playgroud)

等等其他定义.

顺便说一下,记得要有适当的警卫#ifdef:

#ifndef __TTT_H
#define __TTT_H
... header contents
#endif   // __TTT_H
Run Code Online (Sandbox Code Playgroud)