(C ++) Binding to namespaces causes duplicate character error

Over the past few days, I have been trying to figure out how to link files for the CLI game project I was working on. There are two halves of the project: client and server.

The client needs two libraries that I created. The first is a general-purpose game board. This is split between GameEngine.h and GameEngine.cpp. The header file looks something like this:

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
    }
}

And the CPP file just transfers all the content

#include "GameEngine.h"

type gfdGaming::board::functions

The client also has a game code (in this case TicTacToe), divided into declarations and definitions (TTT.h, Client.cpp). TTT.h mostly

#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

I decided to put this in a namespace to group it instead of a class, because there are some parts that will not work well in OOP, and it is much easier to implement later.

, .

: 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*);

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

, , , .

: - , ,

- static extern, , , -, ,

, , =)

+3
4

, , : , .cpp TTT.h, bool PlayerIsX ( nsTicTacToe, ). Server.cpp Client.cpp, .

extern, .cpp (, TTT.cpp).

TTT.h:

namespace nsTicTacToe {
   ...
   extern bool PlayerIsX;
   ...
}

TTT.cpp:

#include "TTT.h"

bool nsTicTacToe::PlayerIsX;

.. .

, , #ifdef s:

#ifndef __TTT_H
#define __TTT_H
... header contents
#endif   // __TTT_H
+6

, ++, C.

extern, "" - .

struct State
{
  GFDCON gfd;
  const char X;
  const char O;
  string MPhostname, mySID;
  board TTTboard;
  bool PlayerIsX, isMyTurn;
  char Player, Player2;
};

Main , .

.

+3

, extern , . , , cpp.

:

extern int somevar;

:

int somevar = ?;

, , , , .

+2
source

you can put the nsTicTacToe namespace in your own .cpp file, compile it separately and link. You may also need a header file that simply declares externs for the variables and includes the client and server .cpp files.

+1
source

Source: https://habr.com/ru/post/1785666/


All Articles