0

I am trying to fix this bug for hour. It is saying that there is multiple definition of the "board" element. Here is the code :

file piece_base.h

#ifndef PIECE_BASE_H
#define PIECE_BASE_H

#include <string>


class piece_base
{
    public:
        piece_base(std::string);
        virtual ~piece_base();
        void position(int,int);
        void kill (piece_base &target) const;
        void getKill();

    protected:
        std::string name;
        bool alive;
        int m_x;
        int m_y;

    private:

};

piece_base *board[8][8];

#endif // piece_base_H

file piece_base.cpp

#include "../include/piece_base.h"

piece_base::piece_base(std::string x)
{
    piece_base::name = x;

    piece_base::m_x = 0;
    piece_base::m_y = 0;
}

piece_base::~piece_base()
{
    //dtor
}

void piece_base::position(int x, int y)
{
piece_base::m_x = x;
piece_base::m_y = y;
}

void piece_base::getKill()
{
    alive = false;
}

void piece_base::kill(piece_base &target) const
{
    target.getKill();
}

file piece_pawn.h

#ifndef PIECE_PAWN_H
#define PIECE_PAWN_H

#include "piece_base.h"

#include <string>

class piece_pawn : public piece_base
{
    public:
        piece_pawn();
        virtual ~piece_pawn();
        void movePiece(int, int);

    protected:
        bool movementRules(int, int);

        bool xOkay(int);
        bool yOkay(int);


    private:

};

#endif // PIECE_PAWN_H

file piece_pawn.cpp

#include "piece_pawn.h"

piece_pawn::piece_pawn() : piece_base("Pawn")
{

}

piece_pawn::~piece_pawn()
{
    //dtor
}

bool piece_pawn::movementRules(int x, int y)
{
    bool z;

    if (xOkay(x) && yOkay(y) == 1)
        z = true;
    else if (xOkay(x) && yOkay(y) == 2)
    {
//        piece_pawn::kill()
    }

    return z;
}

bool piece_pawn::xOkay(int x)
{
    int z;
    if (x == piece_pawn::m_x + 1)
            z = true;
    return z;
}

bool piece_pawn::yOkay(int y)
{
    if (y == piece_pawn::m_y)
        return 1;
    else if (y == piece_pawn::m_y + 1 || y == piece_pawn::m_y - 1)
        return 2;
    else
        return 0;
}


void piece_pawn::movePiece(int x, int y)
{
    if (piece_pawn::movementRules(x, y))
    {
        board[piece_pawn::m_x][piece_pawn::m_y] = NULL;

        piece_pawn::m_x = x;
        piece_pawn::m_y = y;

        board[piece_pawn::m_x][piece_pawn::m_y] = this;

    }
}

and finally :P the file main.cpp

#include "../include/piece_pawn.h"
#include <iostream>

using namespace std;

int main()
{
    piece_pawn pawn1;

    //int boardConverted;

    pawn1.movePiece(1,0);

    for (int x = 0; x < 8; x = x + 1)
    {
        for (int y = 0; y < 8; y = y + 1)
        {
            cout << board[x][y] << "|";
        }
        cout << endl;
    }


    return 0;
}

I dont really know if my question is too complicated but i hope that someone will answer :P

  • `piece_base *board[8][8];` is defined in multiple units – M.M Jan 02 '17 at 04:05
  • what does it really mean? piece_base *board[8][8]; is only on the piece_base.h file... – Félix Archambault Jan 02 '17 at 04:07
  • @FélixArchambault: It is in `piece_base.cpp` and in `piece_pawn.h`(and thus also in `piece_pawn.cpp` and `main.cpp`). So you end up with three `cpp` files containing the `board` variable which means: multiple occasions. – Pixelchemist Jan 02 '17 at 04:12
  • `#include` is a plain text replacement, so it ends up being in multiple units – M.M Jan 02 '17 at 04:14

0 Answers0