编辑:有关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
请张贴实际的代码“私人无效派生::快速排序()'是无效的C++是不是Java此外,后期**最小。 **代码。构造函数和析构函数的问题通常不需要derived.cpp中的所有东西,我必须多次滚动才能看到Derived :: Derived(int)没有被定义。 –
Terribly – Jeff
请包括Base.cpp,因为您在源列表中包含两次Base.h;一次为Base.h,另一次为base.cpp。我们可以看到吗?如果Base ::〜Base()被正确定义(最好是,因为它是在头文件中声明的)。 – WhozCraig