編輯:有關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