1

こんにちは、コードに少し問題があります。

//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 : error LNK2005: "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) 既にクラブで定義されています。オブジェクト

私は彼が何を意味するか知っていますが、date.h でのみコンストラクターを定義し、他の定義がないことを確認します。お願い助けて

4

3 に答える 3

2

そのままでは、Date::Dateコンストラクターは で定義されているため、 、あなたの場合は (少なくとも)およびを含むdate.hすべてで (再) 定義されます。デストラクタも同様です。.cppdate.hclub.cppdate.cppDate::~Date

Date::Date(a)との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()
    {
    }

    //...
于 2016-02-17T19:46:55.730 に答える
1

date.cpp でコンストラクターを定義する必要があります。club.cpp だけでなく club.cpp にも date.h を含めるため (必要に応じて)、club.cpp で 1 回、date.cpp で 1 回定義されます。コンストラクターの宣言だけを date.h に残します。

別の方法は、コンストラクターを宣言する場所で直接定義することです。これは、小さなメンバー関数やコンストラクターに対してのみ行う必要があります。これは、インライン化されるためです。つまり、使用されるすべての場所にコードが挿入され、実行可能ファイルが大きくなります。

于 2016-02-17T19:46:40.173 に答える