g ++对基类析构函数和派生类指针的未定义引用

时间:2016-04-29 05:42:19

标签: c++

编辑:我忘了在各自的.cpp文件中明确定义我的类析构函数。我将*p替换为string *killList = new string[10]; 我的代码现在编译。谢谢你的回复!

我尝试使用以下命令编译以下文件:

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 ++,所以我不确定上面代码出错的地方。我怎样才能编译并运行它?

1 个答案:

答案 0 :(得分: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();
}