error LNK2005 constructor already defined

2.2k Views Asked by At

hello i've got a little problem with my code.

//club.h

#pragma once
#include<iostream>
#include<conio.h>
#include <string>
#include<vector>
#include"Palmares.h"
#include"Stade.h"
#include"Joueur.h"
#include"Date.h"
using namespace std;

class Club
    {
    public:
        Club(int j, int m, int a, int c, string qualite, string n, string addressstade, string nom, string hist, string couleur, string vill, string addressclub);
        ~Club();
        void setNom(string newnom);
        void setHistoire(string newt);
        void setDate(int newj, int newm, int newa);
        void setCouleurClub(string newcouleur);
        void setStade(int newc, string newqualite, string newnom, string newadresse);
        void setVille(string newville);
        void setAddresse(string newadresse);
        string getHistoire()const;
        string getCouleur()const;
        Date getDate()const;
        Stade getStade()const;
        string getVille()const;
        string getAddresse()const;
        vector<Personne*> getTabStaff()const { return tabStaff; };
        vector<Joueur> getTabJoueur()const { return tabJoueur; };
        vector<Palmares> getPalmares()const { return tabPalmares; };

        void addStaff(Personne &newpersonne);
        void addJoueur(Joueur newjoueur);
        void addPalmares(Palmares newpalmares);

    private: 
        string nom_club;
        string histoire;//histoire du club
        string couleur_club;
        Date date;// date de creation
        Stade stade;//stade du club
        string ville; 
        string addresse;
        vector<Personne*> tabStaff;
        vector<Joueur> tabJoueur;//tableau des joueur du club
        vector<Palmares> tabPalmares;//tableau des palmares du club


    };

    //******************************constructeur/destructeur*************************************************
    Club::Club(int j, int m, int a, int c, string qualite, string n, string addressstad, string nom, string hist, string couleur, string vill, string addressclub) 
        : date(j, m, a), stade(c,qualite,n,addressstad)
    {
        nom_club = nom;
        histoire = hist;
        couleur_club = couleur;
        ville = vill;
        addresse = addressclub;
    }

    Club::~Club()
    {
    }

//club.cpp

#include"Club.h"

//*****************************Setteur/getteur************************************************************
void Club::setNom(string newnom){
    nom_club = newnom;
}
void Club::setHistoire(string newt){
    histoire = newt;
}
void Club::setDate(int newj, int newm, int newa){
    date.setJour(newj);
    date.setMois(newm);
    date.setAnne(newa);
}
void Club::setCouleurClub(string newcouleur){
    couleur_club = newcouleur;
}
void Club::setStade(int newc, string newqualite, string newnom, string newadresse){
    stade.setCapacite(newc);
    stade.setQalitePeouse(newqualite);
    stade.setNom(newnom);
    stade.setAdresse(newadresse);
}
void Club::setVille(string newville){
    ville = newville;
}
void Club::setAddresse(string newadresse){
    addresse = newadresse;
}
string Club::getHistoire()const{
    return histoire;
}
string Club::getCouleur()const{
    return couleur_club;
}
Date Club::getDate()const{
    return date;
}
Stade Club::getStade()const{
    return stade;
}
string Club::getVille()const{
    return ville;
}
string Club::getAddresse()const{
    return addresse;
}

/****************************************fonction d'ajout des joueur, staff et palmares*************************************************/
void Club::addStaff(Personne &newpersonne){
    tabStaff.push_back(&newpersonne);
}
void Club::addJoueur(Joueur newjoueur){
    tabJoueur.push_back(newjoueur);
}
void Club::addPalmares(Palmares newpalmares){
    tabPalmares.push_back(newpalmares);
}

//date.h

#pragma once
#include<iostream>
#include<conio.h>

using namespace std;


class Date
{
public:
    Date(int j, int m, int a);
    ~Date();
    void afficher();
    void setJour(int newj);
    void setMois(int newm);
    void setAnne(int newa);
    int getJour()const;
    int getMois()const;
    int getAnne()const;
private:
    int jour;
    int mois;
    int annee;
};

Date::Date(int j, int m, int a)
{
    jour = j;
    mois = m;
    annee = a;
}

Date::~Date()
{
}

//date.cpp

#include"Date.h"

void Date::afficher(){
    cout << jour << '/' << mois << '/' << annee << endl;
}

void Date::setJour(int newj){
    jour = newj;
}
void Date::setMois(int newm){
    mois = newm;
}
void Date::setAnne(int newa){
    annee = newa;
}

int Date::getJour()const{
    return jour;
}
int Date::getMois()const{
    return mois;
}
int Date::getAnne()const{
    return annee;
}

and my problem is VS throw me this 1>Date.obj : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) already defined in Club.obj

i know what he would mean but i have defined the constructor only on date.h and i check there is no other definition. help me please

3

There are 3 best solutions below

1
On BEST ANSWER

As it is, the Date::Date constructor is defined in date.h and will therefore be (re)defined in any .cpp that includes date.h, in your case (at least) club.cpp and date.cpp. Same goes for the Date::~Date destructor.

You can either (a) move the Date::Date and Date::~Date definitions to date.cpp, or (b) define them as inline in date.h:

class Date
{
public:
    Date::Date(int j, int m, int a)
    {
        jour = j;
        mois = m;
        annee = a;
    }

    Date::~Date()
    {
    }

    //...
0
On

Just move the definitions inside the class so that they are inlined:

class Date
{
public:
    Date(int j, int m, int a)
    {
        jour = j;
        mois = m;
        annee = a;
    }
    ~Date()
    {
    }
    void afficher();
    void setJour(int newj);
    void setMois(int newm);
    void setAnne(int newa);
    int getJour()const;
    int getMois()const;
    int getAnne()const;
private:
    int jour;
    int mois;
    int annee;
};
1
On

You should define the constructor in date.cpp. Since you include date.h in club.cpp as well as in club.cpp (as you should), it is defined once in club.cpp and once in date.cpp. Leave only the constructor's declaration in date.h.

An alternative is to define the constructor directly where you declare it. This you should do only for small member functions / or constructors, since they are then inlined, which means the code is inserted everywhere it is used which makes your executable file bigger.