0

我有一个接口,我想创建一个header带有函数的函数interface和一个.cpp实现这个头文件中的函数。但是当尝试这个时,我总是undefined reference to 'vtable for Test'Testt.h文件中遇到问题。

我正在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_ */

测试.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;
}
};

所以我不明白为什么如果我使用标题(我认为这是更好的解决方案)它不起作用。

我希望我能清楚地解释我的问题是什么。如果没有请询问。

4

2 回答 2

3

您正在定义一个非成员函数int test()Testt.cpp您需要定义int Test::test()

int Test::test()
{// ^^^^
  return 0;
}
于 2013-08-05T10:21:29.537 回答
1

对 X 的未定义引用意味着链接器找不到已声明的 X 的定义。

你声明Test有一个成员函数int test()但是这个

int test() {
   return 0;
}

定义一个自由函数。

你需要

int Test::test() {
    return 0;
}

undefined reference to 'vtable for Test'"测试有点混乱。这通常意味着您忘记定义类的第一个虚函数

于 2013-08-05T10:21:55.703 回答