Redefintion Errors

I am working on a program that simulates a game of GoFish. I have a weird error that is making it difficult to make any progress. I get the following error when building "error: redefinition of 'Player'." I have looked at various websites regarding the error, but it seems as if all of the code includes the cpp in the header and the header in the cpp. However, mine does not.

Here is the .cpp file:

 #include "player.h"

 Player::Player(){

    myName = "";

}

Player::Player(string name) {

    myName = name;

}

string Player::getName() const {

    return myName;

}

I have excluded the methods which aren't giving errors.

Additionally, here is the .h file:

#ifndef UNTITLED3_PLAYER_H
#define UNTITLED3_PLAYER_H


#include <iostream>
#include <string>
#include <vector>


#include "card.h"
using namespace std;


class Player
{
public:
     Player();

     Player(string name) {
        myName = name;
     }

    string getName() const {
       return myName;
    }

    void addCard(Card c);  //adds a card to the hand
    void bookCards(Card c1, Card c2);
    bool checkHandForBook(Card &c1, Card &c2);
    bool rankInHand(Card c) const;
    Card chooseCardFromHand() const;
    bool cardInHand(Card c) const;
   Card removeCardFromHand(Card c);

    string showHand() const;
    string showBooks() const;

   int getHandSize() const;
   int getBookSize() const;

private:

    vector <Card> myHand;
    vector <Card> myBook;

    string myName;

};


#endif

The only two function which are getting redefinition errors are the constructor and getName. What could be causing this?

1 answer

  • answered 2018-11-08 05:35 user1118321

    You've included the entire constructor and getName() function in the header and in the .cpp file. They should only exist in one or the other.