Ошибка конструктора LNK2005 уже определена

Привет у меня небольшая проблема с моим кодом

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

и моя проблема заключается в том, что VS бросил мне это 1>Date.obj: ошибка LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) уже определено в Club. OBJ

я знаю, что он имел бы в виду, но я определил конструктор только на date.h, и я проверяю, что другого определения нет. Помоги мне, пожалуйста

3 ответа

Решение

Как это, Date::Date конструктор определяется в date.h и поэтому будет (пере) определяться в любом .cpp это включает date.hв твоем случае (как минимум) club.cpp а также date.cpp, То же самое касается Date::~Date деструктор.

Вы можете либо (а) переместить Date::Date а также Date::~Date определения к date.cppили (б) определить их как встроенные в date.h:

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

    Date::~Date()
    {
    }

    //...

Просто переместите определения внутри класса так, чтобы они были встроены:

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

Вы должны определить конструктор в date.cpp. Поскольку вы включаете date.h в club.cpp, а также в club.cpp (как вам следует), он определяется один раз в club.cpp и один раз в date.cpp. Оставьте только объявление конструктора в date.h.

Альтернатива - определить конструктор непосредственно там, где вы его объявили. Это следует делать только для небольших функций-членов / конструкторов, поскольку они затем вставляются, что означает, что код вставляется везде, где он используется, что увеличивает размер исполняемого файла.

Другие вопросы по тегам