Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: cNoob en 20 Enero 2018, 22:31 pm



Título: Error de definición múltiple
Publicado por: cNoob en 20 Enero 2018, 22:31 pm
Hola!
Cada vez que hago una clase en CodeBlocks y trato de incluirla en el archivo header de otra clase, el compilador me da un error de declaración múltiple en todos los métodos y no se a que se puede deber ya que estos solo son definidos una vez y todos los headers tienen al principio el #ifndef
Mi programa:

TicTacToeEngine.h
Código
  1. #ifndef TICTACTOEENGINE_H
  2. #define TICTACTOEENGINE_H
  3.  
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class TicTacToeEngine
  8. {
  9. protected:
  10.    vector <unsigned short> board;
  11.    unsigned short state;
  12.    bool first__player_turn;
  13.  
  14.    void Check();
  15.  
  16. public:
  17.    TicTacToeEngine(void);
  18.    TicTacToeEngine( const vector<unsigned short>);
  19.    TicTacToeEngine( const TicTacToeEngine&);
  20.    ~TicTacToeEngine(void);
  21.    TicTacToeEngine operator=( const TicTacToeEngine&);
  22.  
  23.    bool Movement(unsigned short);
  24.    vector<unsigned short> Board(void) const;
  25.    void Restart(void);
  26.    unsigned short State(void) const;
  27.    bool FirstPlayer(void) const;
  28.  
  29. };
  30.  
  31. #endif // TICTACTOEENGINE_H
  32.  

TicTacToeEngine.cpp
Código
  1. #include "TicTacToeEngine.h"
  2.  
  3. //////////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. // win checker
  6.  
  7. //checks if there is a line
  8. void TicTacToeEngine::Check(void)
  9. {
  10.    /////////////
  11.    //CHECK TIE//
  12.    /////////////
  13.  
  14.    unsigned short i = 0;
  15.  
  16.    while(i < 9 && board[i] != 0) {
  17.  
  18.        if(i == 8) {
  19.            state = 3;
  20.            return;
  21.        }
  22.        i++;
  23.    }
  24.  
  25.    //////////////////////
  26.    //CHECK FIRST PLAYER//
  27.    //////////////////////
  28.  
  29.    //CHECK HORIZONTAL
  30.  
  31.    //first line
  32.    if(board[0] == 1 && board[1] == 1 && board[2] == 1)
  33.        state = 1;
  34.        return;
  35.  
  36.    //second line
  37.    if(board[3] == 1 && board[4] == 1 && board[5] == 1)
  38.        state = 1;
  39.        return;
  40.  
  41.    //third line
  42.    if(board[6] == 1 && board[7] == 1 && board[8] == 1)
  43.        state = 1;
  44.        return;
  45.  
  46.    //CHECK VERTICAL
  47.  
  48.    //first line
  49.    if(board[0] == 1 && board[3] == 1 && board[6] == 1)
  50.        state = 1;
  51.        return;
  52.  
  53.    //second line
  54.    if(board[1] == 1 && board[4] == 1 && board[7] == 1)
  55.        state = 1;
  56.        return;
  57.  
  58.    //third line
  59.    if(board[2] == 1 && board[5] == 1 && board[8] == 1)
  60.        state = 1;
  61.        return;
  62.  
  63.    //DIAGONAL LINES
  64.  
  65.    //top right to bottom left
  66.    if(board[2] == 1 && board[4] == 1 && board[6] == 1 )
  67.        state = 1;
  68.        return;
  69.  
  70.    //top left to bottom right
  71.    if(board[0] == 1 && board[4] == 1 && board[8] == 1 )
  72.        state = 1;
  73.        return;
  74.  
  75.    ///////////////////////
  76.    //CHECK second PLAYER//
  77.    ///////////////////////
  78.  
  79.    //CHECK HORIZONTAL
  80.  
  81.    //first line
  82.    if(board[0] == 2 && board[1] == 2 && board[2] == 2 )
  83.        state = 2;
  84.        return;
  85.  
  86.    //second line
  87.    if(board[3] == 2 && board[4] == 2 && board[5] == 2 )
  88.        state = 2;
  89.        return;
  90.  
  91.    //third line
  92.    if(board[6] == 2 && board[7] == 2 && board[8] == 2 )
  93.        state = 2;
  94.        return;
  95.  
  96.    //CHECK VERTICAL
  97.  
  98.    //first line
  99.    if(board[0] == 2 && board[3] == 2 && board[6] == 2 )
  100.        state = 2;
  101.        return;
  102.  
  103.    //second line
  104.    if(board[1] == 2 && board[4] == 2 && board[7] == 2 )
  105.        state = 2;
  106.        return;
  107.  
  108.    //third line
  109.    if(board[2] == 2 && board[5] == 2 && board[8] == 2 )
  110.        state = 2;
  111.        return;
  112.  
  113.    //DIAGONAL LINES
  114.  
  115.    //top right to bottom left
  116.    if(board[2] == 2 && board[4] == 2 && board[6] == 2 )
  117.        state = 2;
  118.        return;
  119.  
  120.    //top left to bottom right
  121.    if(board[0] == 2 && board[4] == 2 && board[8] == 2 )
  122.        state = 2;
  123.        return;
  124.  
  125.    return;
  126. }
  127.  
  128.  
  129. //////////////////////////////////////////////////////////////////////////////////////////
  130.  
  131. // constructors & destructor
  132.  
  133. //normal constructor
  134. TicTacToeEngine::TicTacToeEngine(void) :
  135.    state(0), first__player_turn(false)
  136. {
  137.    //creates an array filled with 0 (empty board)
  138.    for(int i = 0; i < 9; i++) {
  139.        board.push_back(0);
  140.    }
  141. }
  142.  
  143. //constructor with vector as argument
  144. TicTacToeEngine::TicTacToeEngine( const vector<unsigned short> original_board) :
  145.    board(original_board), first__player_turn(false)
  146. {
  147.    Check();
  148. }
  149.  
  150. //copy constructor
  151. TicTacToeEngine::TicTacToeEngine( const TicTacToeEngine& original) :
  152.    board( original.board), state(original.state),
  153.    first__player_turn(original.first__player_turn) {}
  154.  
  155. TicTacToeEngine::~TicTacToeEngine(void) {}
  156.  
  157.  
  158. //////////////////////////////////////////////////////////////////////////////////////////
  159.  
  160. // operator =
  161.  
  162. TicTacToeEngine TicTacToeEngine::operator=( const TicTacToeEngine& original)
  163. {
  164.    board = original.board;
  165.    state = original.state;
  166.    first__player_turn = original.first__player_turn;
  167.  
  168.    return *this;
  169. }
  170.  
  171.  
  172. //////////////////////////////////////////////////////////////////////////////////////////
  173.  
  174. // Movement method
  175.  
  176. bool TicTacToeEngine::Movement(unsigned short place)
  177. {
  178.    //if the place is not between 0 and 8 or occupied or if the game has finished returns false
  179.    if( place < 0 || place > 8 || board[place] != 0 || state != 0)
  180.        return false;
  181.  
  182.    //else the place in the board is filled
  183.    if(first__player_turn) {
  184.        board[place] = 1;
  185.    }
  186.  
  187.    else {
  188.        board[place] = 2;
  189.    }
  190.  
  191.    //check if the game has ended
  192.    Check();
  193.    first__player_turn = !first__player_turn;
  194.  
  195.    return true;
  196. }
  197.  
  198.  
  199. //////////////////////////////////////////////////////////////////////////////////////////
  200.  
  201. // Return board method
  202.  
  203. vector<unsigned short> TicTacToeEngine::Board(void) const
  204. {
  205.    return board;
  206. }
  207.  
  208.  
  209. //////////////////////////////////////////////////////////////////////////////////////////
  210.  
  211. // Restart method
  212.  
  213. void TicTacToeEngine::Restart(void)
  214. {
  215.    //restarts the board array with all elements at 0
  216.    board.clear();
  217.  
  218.    for(int i = 0; i < 9; i++) {
  219.        board.push_back(0);
  220.    }
  221.  
  222.    //restarts game data
  223.    state = 0;
  224.    first__player_turn = true;
  225. }
  226.  
  227.  
  228. //////////////////////////////////////////////////////////////////////////////////////////
  229.  
  230. // Returns true if the game has finished
  231.  
  232. unsigned short TicTacToeEngine::State(void) const
  233. {
  234.    return state;
  235. }
  236.  
  237.  
  238. //////////////////////////////////////////////////////////////////////////////////////////
  239.  
  240. // Returns true if it is the turn of the first player
  241.  
  242. bool TicTacToeEngine::FirstPlayer(void) const
  243. {
  244.    return first__player_turn;
  245. }
  246.  
  247.  
  248. //////////////////////////////////////////////////////////////////////////////////////////
  249.  

TicTacToe.h
Código
  1. #ifndef TICTACTOE_H
  2. #define TICTACTOE_H
  3.  
  4. #include <iostream>
  5. #include <windows.h>
  6. #include "TicTacToeEngine.h"
  7.  
  8. using namespace std;
  9.  
  10. class TicTacToe : public TicTacToeEngine
  11. {
  12.    void Graphic(unsigned short);
  13.  
  14. public:
  15.    TicTacToe(void);
  16.    bool GraphicMovement(void);
  17.    void Play(void);
  18. };
  19.  
  20. /*
  21.  
  22. base class methods:
  23.  
  24.     TicTacToeEngine(void);
  25.     TicTacToeEngine( const vector<unsigned short>);
  26.     TicTacToeEngine( const TicTacToeEngine&);
  27.     ~TicTacToeEngine(void);
  28.     TicTacToeEngine operator=( const TicTacToeEngine&);
  29.  
  30.     bool Movement(unsigned short);
  31.     vector<unsigned short> Board(void) const;
  32.     void Restart(void);
  33.     unsigned short State(void) const;
  34.     bool FirstPlayer(void) const;
  35.  
  36. */
  37.  
  38. #endif // TICTACTOE_H

TicTacToe.cpp
Código
  1. #include "TicTacToe.h"
  2.  
  3. //////////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. // constructors & destructor
  6.  
  7. //normal constructor
  8. TicTacToeEngine::TicTacToeEngine(void) :
  9.    state(0), first__player_turn(false)
  10. {
  11.    //creates an array filled with 0 (empty board)
  12.    for(int i = 0; i < 9; i++) {
  13.        board.push_back(0);
  14.    }
  15. }
  16.  
  17.  
  18. //////////////////////////////////////////////////////////////////////////////////////////
  19.  
  20. // Graphic Movement
  21.  
  22. void TicTacToe::Graphic(unsigned short i)
  23. {
  24.    if(board[i] == 0) {
  25.        cout << " ";
  26.    }
  27.  
  28.    if(board[i] == 1) {
  29.        cout << "x";
  30.    }
  31.  
  32.    if(board[i] == 2) {
  33.        cout << "O";
  34.    }
  35. }
  36.  
  37. bool TicTacToe::GraphicMovement(void)
  38. {
  39.    system("CLS");
  40.  
  41.    cout << endl << endl;
  42.    cout << "   "; Graphic(0); cout << " | "; Graphic(1); cout << " | "; Graphic(2); cout << endl;
  43.    cout << "  ------------" << endl;
  44.    cout << "   "; Graphic(3); cout << " | "; Graphic(4); cout << " | "; Graphic(5); cout << endl;
  45.    cout << "  ------------" << endl;
  46.    cout << "   "; Graphic(6); cout << " | "; Graphic(7); cout << " | "; Graphic(8); cout << endl;
  47.    cout << endl << endl;
  48.  
  49.    cout << "Movimiento del jugador";
  50.  
  51.    if(first__player_turn)
  52.        cout << " 1: ";
  53.  
  54.    if(!first__player_turn)
  55.        cout << " 2: ";
  56.  
  57.    unsigned short movement;
  58.    bool moved;
  59.    cin >> movement;
  60.  
  61.    moved = Movement(movement);
  62.  
  63.    return moved;
  64. }
  65.  
  66.  
  67. //////////////////////////////////////////////////////////////////////////////////////////
  68.  
  69. //Just play
  70.  
  71. void TicTacToe::Play(void)
  72. {
  73.    while(state == 0) {
  74.        GraphicMovement();
  75.    }
  76.  
  77.    cout << endl << endl << "End of the game." << endl << endl << "Result: ";
  78.  
  79.    if(state == 1) {
  80.        cout << "First player has won." << endl << endl;
  81.    }
  82.  
  83.    if(state == 2) {
  84.        cout << "Second player has won." << endl << endl;
  85.    }
  86.  
  87.    if(state == 3) {
  88.        cout << "Tie" << endl << endl;
  89.    }
  90. }
  91.  
  92.  
  93. //////////////////////////////////////////////////////////////////////////////////////////
  94.  


Título: Re: Error de definición múltiple
Publicado por: user-marcos en 21 Enero 2018, 00:19 am
Estoy tiempo sin tocar c, pero si no recuerdo mal, el problema está cuando extiendes la clase TicTacToeEngine. Hay varias soluciones, una rápida es que definas las funciones de la clase TicTacToeEngine, en la cabecera empleando métodos inline.