编辑:与c++ undefined reference to `vtable相关
我正在尝试继承一个项目,我收到了这个错误:
/tmp/ccw1aT69.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)'
/tmp/ccw1aT69.o: In function `Derived::~Derived()':
main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x13): undefined reference to `vtable for Derived'
main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x1f): undefined reference to `Base::~Base()'
collect2: ld returned 1 exit status
这是我的代码:
main.cpp中:
#include <iostream>
#include "Base.h"
#include "Derived.h"
int main() {
Derived intList(25);
}
base.h:
#ifndef BASE_H
#define BASE_H
class Base {
public:
...
Base (const Base& otherList);
virtual ~Base();
protected:
int *list;
int length;
int maxSize;
};
#endif
Base.cpp:
#include "Base.h"
#include <iostream>
using namespace std;
...definitions of my members...
Base::Base (int size) {
//stuff
}
Base::~Base() {
delete [] list;
}
Base::Base (const Base& otherList) {
//stuff
}
Derived.h:
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived: public Base {
public:
...
Derived (int size = 100);
~Derived(); //THIS LINE ADDED AFTER FIRST ANSWER
};
#endif
Derived.cpp:
#include "Derived.h"
#include <iostream>
using namespace std;
Derived::Derived (int size)
:Base(size){
}
导致此错误的原因是什么?看起来我无法调用构造函数,但它看起来很好。
编辑:我尝试了第一个解决方案。现在出错:/tmp/ccA4XA0B.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)'
main.cpp:(.text+0x21): undefined reference to `Derived::~Derived()'
collect2: ld returned 1 exit status
答案 0 :(得分:7)
您已在Base
中声明了虚拟析构函数,但您从未定义它。它需要在Derived
(以及Base
中定义,因为它不是纯虚函数),因为它会在main
退出时调用。你应该:
class Base {
public:
// ...
virtual ~Base();
};
Base::~Base() {}
class Derived : public Base {
public:
// ...
~Derived();
};
Derived::~Derived() { /* whatever */ }
这至少是您的一个错误的原因。我不知道这个是不是红鲱鱼,但似乎是:
/tmp/ccw1aT69.o:在函数
main': main.cpp:(.text+0x15): undefined reference to
Derived :: Derived(int)'
您定义Derived::Derived(int)
,因此很难想象这是一个真正的错误。定义你的析构函数,看看它是否会消失。
答案 1 :(得分:3)
好的,只是为了清楚起见,因为除了答案之外我不能将格式化的代码放在任何东西上。您不需要在派生类中提供析构函数,因为您的基类具有虚拟dtor或纯方法。以下是关于在三种不同的构造/破坏条件下证明这一点的简单方法。输出在代码后面列出。我希望这至少有助于@Jeff。我在VS2005 / 2008/2010和一个古老的gcc 4.1.2下进行了测试,所以最好是正确的。
#include <iostream>
class Base {
public:
Base()
{ std::cout << "Base()" << std::endl; };
virtual void call_me() = 0;
virtual ~Base()
{ std::cout << "~Base()" << std::endl << std::endl; };
};
class Derived : public Base {
public:
Derived(int i=1)
{ std::cout << "Derived(" << i << ")" << std::endl; }
// Base::call_me requirements.
void call_me()
{ std::cout << "call_me()" << std::endl; }
};
int main(int argc, char* argv[])
{
// use derived class pointer type
Derived* pDerived = new Derived();
pDerived->call_me();
delete pDerived;
// use base class pointer type
Base* pBase = new Derived(2);
pBase->call_me();
delete pBase;
// scope based
{
Derived obj(3);
obj.call_me();
}
return 0;
}
这个输出是:
Base()
Derived(1)
call_me()
~Base()
Base()
Derived(2)
call_me()
~Base()
Base()
Derived(3)
call_me()
~Base()
答案 2 :(得分:0)
main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)'
此错误消息未提及对类的未定义引用,因为标题问题如此。相反,它抱怨对构造函数的未定义引用,它引用了一个int参数。从我粗略看一下你的代码,你只声明了这个构造函数而没有定义它。在.cpp文件中添加定义,您应该解决此错误。
此外,通常的做法是在所有成员函数声明之前放置构造函数声明。起初我错过了这些声明,因为它们不是我所期望的。我强烈建议你这样做,以便在这里提出问题时避免将来的误解。