2016-04-29 16 views
0

編集:クラスのデストラクタをそれぞれの.cppファイルで明示的に定義することを忘れてしまいました。私は*pstring *killList = new string[10]; に置き換え、私のコードがコンパイルされました。あなたの返信をありがとう!g ++基本クラスのデストラクタと派生クラスポインタへの未定義の参照

私はコマンドを使用して、次のファイルをコンパイルしようとしました:

g++ -o hunter hunter_h.h hunter_h.cpp animal_h.h animal_h.cpp main.cpp 

animal_h.h

#include <iostream> 
#include <string> 
#ifndef ANIMAL_H 
#define ANIMAL_H 
using namespace std; 

// Animal class 
class animal 
{ 
    friend class hunter; 
    // need a name, species, private ID 
public: 
    animal(); 
    animal(string aSpecies); 
    string name; 
    string species; 
    string getSpecies(); 
    void setName(string aName); 
    string getName(); 
    int getID(); 
    ~animal(); 

private: 
    static int uID; 

}; 



#endif 

animal_h.cpp

#include "animal_h.h" 
//#include "hunter_h.h" 
#include <iostream> 
#include <string> 
using namespace std; 
int animal:: uID = 0 ; 



animal::animal() 
{ 
    cout << "Created an animal!" << endl; 
    name = "?"; 
    species = "?"; 
    uID++; 
} 



animal::animal(string aSpecies) 
{ 
    cout << "Created 1 "<< aSpecies << "!" << endl; 
    name= "?"; 
    species = aSpecies; 
    uID++; 
} 




string animal::getSpecies() 
{ 
    cout << species << endl; 
} 



void animal::setName(string aName) 
{ 
    name = aName; 
    cout << "This " << species << " is now called " << name << endl; 
} 


string animal::getName() 
{ 
    cout << name << endl; 
} 


int animal:: getID() 
{ 
    cout << uID << endl; 
} 

hunter_h.hこれはユニークな振る舞いを持つ動物基底クラスの派生クラスです。

#include "animal_h.h" 
#include <iostream> 
#ifndef ANIMAL_HUNTER 
#define ANIMAL_HUNTER 

class hunter : public animal 
{ 
public: 
    hunter(); 
    hunter(std::string aSpecies); 
    void recordKills(std::string kill); 
    static int tKills; 
    int totalKills(); 
    static std::string *theKills(); 
    static std::string *p; 
    static int clearTotal(); 
    ~hunter(); 
}; 
#endif 

hunter_h.cpp今

#include "animal_h.h" 
#include "hunter_h.h" 
#include <iostream> 
#include <string> 
using namespace std; 
int hunter:: tKills =0; 
string killList[10]; 

hunter::hunter() 
{ 
    cout<<"created a hunter! "<<endl; 
    name= "? "; 
    species="? "; 
    string *p; 
    p = &killList[0]; 


} 

hunter::hunter(string aSpecies) 
{ 
    name = "?"; 
    species = aSpecies; 
    cout << "created a hunting "<<species <<endl; 


} 

string *theKills() 
{ 
    return hunter::p; 
} 

void hunter::recordKills(string kill) 
{ 
    cout << kill << " killed." << endl; 
    *(p+tKills) = kill; 
    tKills++; 
    cout << tKills << " total kills." << endl; 

} 

int hunter::totalKills() 
{ 
    cout << name << "'s " << "Total kills: " << tKills << endl; 
} 
int hunter::clearTotal() 
{ 
    delete[] killList; 
    return 0; 
} 

main.cppに

#include "animal_h.h" 
#include "hunter_h.h" 
#include <iostream> 
#include <string> 
using namespace std; 
int main() 
{ 

    hunter *hunterC; 
    hunterC= new hunter("cheetah"); 
    hunterC->recordKills("Mouse"); 
    hunterC-> recordKills("Gazelle, Gazelle"); 
    hunterC-> recordKills("Hyena"); 
    hunterC-> recordKills("Rabbit, Rabbit"); 
    hunterC->theKills; 
    hunterC->clearTotal; 



} 

、私がコンパイルしようとすると、私は次の警告やエラーを取得:

hunter_h.cpp: In static member function ‘static int hunter::clearTotal()’: 
hunter_h.cpp:49:11: warning: deleting array ‘killList’ 
    delete[] killList; 
     ^
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x71): undefined reference to `animal::~animal()' 
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x101): undefined reference to `animal::~animal()' 
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x119): undefined reference to `hunter::p' 
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x15a): undefined reference to `hunter::p' 
/tmp/ccqCD1e7.o:main.cpp:(.text+0x1f4): undefined reference to `animal::~animal()' 
/tmp/ccqCD1e7.o:main.cpp:(.text+0x20c): undefined reference to `animal::~animal()' 
collect2: error: ld returned 1 exit status 

私は数ヶ月間だけC++を学んできましたので、上記のコードがどこに間違っているのか分かりません。これをコンパイルして実行するにはどうすればよいですか?

+3

動物のデストラクタを明示的に宣言する場合は、.cppファイルで定義する必要があります。 – piyushj

+0

ありがとう、私はそれを変更し、hunter :: pエラーへの定義されていない参照を取得するだけです – PDL

+0

'hunter :: p'の使い方は私を混乱させます。私はあなたがそれでやろうとしていることを理解しているとは思わない。それはなんのためですか?なぜそれは '静的'ですか?なぜそれは '文字列'ではなく '文字列'ではないのですか? –

答えて

0

killListがnew []を使用して割り当てられていない場合、delete []で削除しないでください。実行時に割り当てられていないため、明示的に割り当てを解除する必要はありません。プログラムの終了時にメモリを解放します。コードをそのまま使用すると、killList配列をオーバーランさせる可能性があります。

代わりにstd :: vectorを使用してみてください。

std::vector<std::string> killList; 

... 

void recordKills(std::string s) 
{ 
... 
    killList.push_back(s); 
} 

void clearTotal() 
{ 
    killList.clear(); 
} 
関連する問題