c++:使用标头时未定义对vtable的引用,但不能没有
c++: undefined reference to vtable when using header but not without
我有一个接口,我想用interface
中的函数创建一个header
,用.cpp
实现这个头中的函数。但当尝试此操作时,我总是在Testt.h
文件中遇到问题undefined reference to 'vtable for Test'
。
我正在eclipse中做一个相当大的项目,所以我把问题归结为几个小类。
ITestAdapter.h:
#ifndef ITESTADAPTER_H_
#define ITESTADAPTER_H_
class TestAdapter {
public:
virtual int test() = 0;
};
#endif /* ITESTADAPTER_H_ */
测试h:
#ifndef TESTT_H_
#define TESTT_H_
#include "ITestAdapter.h"
class Test: public TestAdapter{
public:
virtual int test();
};
#endif /* TESTT_H_ */
Testt.cpp:
#include "Testt.h"
int test() {
return 0;
}
Test_main.cpp:
#include <iostream>
#include "Testt.h"
using namespace std;
int main() {
Test t;
int i = t.test();
cout << i << endl;
return 0;
}
如果我根本不使用Testt.h
,而是在Testt.cpp
中实现接口,并用我的主方法将Testt.cpp
(我想避免的)包含在文件中,那么它可以正常工作。
Testt.cpp(已修改):
#include "ITestAdapter.h"
class Test: public TestAdapter {
public:
int test() {
return 0;
}
};
所以我不明白为什么如果我使用标头(我认为这是更好的解决方案),它就不起作用。
我希望我能解释清楚我的问题是什么。如果不能,请问。
您正在Testt.cpp
中定义一个非成员函数int test()
。您需要定义int Test::test()
:
int Test::test()
{// ^^^^
return 0;
}
对X的未定义引用意味着链接器找不到已声明的X的定义。
您声明Test
具有成员函数int test()
,但此
int test() {
return 0;
}
定义了一个自由函数。
你需要
int Test::test() {
return 0;
}
undefined reference to 'vtable for Test'"
测试有点令人困惑。这通常意味着您忘记定义类的第一个虚拟函数。
相关文章:
- 纯虚拟类和错误未定义对 'vtable 的引用
- 对植物的 vtable 的未定义引用?
- 使用多个命名空间对 vtable 的未定义引用
- 对"车辆的 vtable"的未定义引用 - 面向对象的编程练习
- Qt Quick + CMake + 自定义 QObject 导致对"vtable"的未定义引用
- C++ 未定义对 vtable 异常的引用
- 为每个抽象函数定义主体后,对VTable的不确定引用
- 看起来如此主要的错误.cpp:(.text.startup+0xd6):未定义对"vtable for Counter"的引用?
- 用嵌套类对VTable的未定义引用
- 未定义对 vtable 错误的引用
- 对'vtable for class'的未定义引用
- 奇怪的未定义引用了`vtable
- 构造函数中对"vtable"的未定义引用
- C++ 继承问题:未定义对"vtable"的引用
- 构造函数中对 vtable 的未定义引用
- 在 qt 控制台应用程序中未定义对 'vtable for myObj' 的引用 - 信号和插槽
- 对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用
- 对具有继承的 vtable 的未定义引用
- 在Windows上为Qt 5.0.1 32位构建QMYSQL时,未定义引用' vtable for QMySQLDri
- OpenCV 未定义引用 'vtable for cv::HOGDescriptor'