2012-03-30 14 views
0

同じメソッド名で2つのクラスを作成しようとしています。 その練習なので、行動を変更することはできません。静的クラスと継承

Person.h

#ifndef __PERSON__ 
#define __PERSON__ 
#include <iostream> 

using namespace std; 


class person{ 
protected: 
    string name; 
    static int quantity; 
private: 

public: 
    person(); 
    ~person(); 
    string getName() const; 

    static void add(); 
    static int getQuantity(); 

}; 
#endif 

person.cpp

#include "person.h" 

int person::quantity=0; 
person::person(){} 
person::~person(){} 
string person::getName() const{ 
    return this->name; 
} 


int person::getQuantity(){ 
    return person::quantity; 
} 

user.h

#ifndef __USER__ 
#define __USER__ 
#include <iostream> 
#include "person.cpp" 

using namespace std; 


class user:public person{ 
private: 
    int age; 
    static int quantity; 

public: 
    user(); 
    ~user(); 

    static int getQuantity(); 
    static void add(); 

    int getAge(); 
    void setAge(int age); 


};    
#endif 

user.cpp

#include "user.h" 

int user::quantity=0; 

user::user():person(){} 
user::~user(){} 

int user::getQuantity(){ 
    return user::quantity; 
} 
void user::add(){ 
    user::quantity++; 
} 
int user::getAge(){ 
    return this->age; 
} 
void user::setAge(int age){ 
    if(age>=0)this->age=age; 
} 

問題はldです:/var/folders/bg/171jl37d05v69c4t6t1tt03m0000gn/T//ccRJU6B9.oと/var/folders/bg/171jl37d05v69c4t6t1tt03m0000gn/T//ccVVSd1i.oアーキテクチャx86_64の場合、ld:duplicate symbol person :: getQuantity()が重複しています collect2:ldは1の終了ステータスを返しました

私はその特定のクラスの静的メソッドを作成します。どうすればそれを解決できますか?解決

+2

二重アンダースコアを含む識別子は、実装のために予約されています。これはマクロ名にも当てはまります。 –

+1

'using namespace std;'はヘッダーにも不正な形式です。 – Mat

答えて

0

...問題は、問題はあなたがuser.h.の初めにperson.cppを含めていることにあるの#include「person.cpp」

0

ましたこれにより、person.cppの内容が2回コンパイルされるため、クラスのすべてのメンバーの定義が2つありません。あなたは、おそらく間違っている

2

あなた

#include "person.cpp" 

person.h含むことを意味します。それは2回コンパイルされます。

おそらく#include "person.h"にします。

0

#include "person.cpp"User.hに含めています。

これを#include "person.h"に変更してください。すべて正常です。

この変更を加えた後、Visual Studio 2010でコードをコンパイルしようとしましたが、すべてがうまくコンパイルされました。