错误LNK2005构造函数已定义 [英] error LNK2005 constructor already defined

查看:153
本文介绍了错误LNK2005构造函数已定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的代码有点问题。



// club.h

  #pragma once 
#include< iostream>
#include< conio.h>
#include< string>
#include< vector>
#includePalmares.h
#includeStade.h
#includeJoueur.h
#includeDate.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;
日期日期; //创作日期
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 (j,m,a)
: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

  #includeClub.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;
};

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

Date ::〜Date()
{
}

// date.cpp

  #includeDate.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:error LNK2005:已在Club.obj中定义的public:__thiscall Date :: Date(int,int,int)(?? 0Date @@ QAE @ HHH @ Z)



我知道他的意思,但我已经定义了构造函数只有在date.h
和我检查没有其他定义。
帮我请

解决方案

因为 Date :: Date 构造函数在 date.h 中定义,因此将在任何 .cpp 包括 date.h ,在您的情况下(至少) club.cpp 日期。 cpp Date ::〜Date 析构函数也是如此。



您可以(a)移动 Date :: Date Date ::〜Date 定义到 date.cpp ,或(b)在 date.h 中将它们定义为内联:

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

Date ::〜Date()
{
}

// ...


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

解决方案

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()
    {
    }

    //...

这篇关于错误LNK2005构造函数已定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆