gpt4 book ai didi

c++:使用 header 但并非没有时对vtable的 undefined reference

转载 作者:行者123 更新时间:2023-11-27 23:13:45 28 4
gpt4 key购买 nike

<分区>

我有一个接口(interface),我想用 interface 中的函数创建一个 header 和一个实现函数的 .cpp header 。但是当尝试这个时,我总是在 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_ */

测试.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 中实现接口(interface)并包含 Testt.cpp (我想避免的)在我的主要方法文件中然后它工作正常。

Testt.cpp(已修改):

#include "ITestAdapter.h"
class Test: public TestAdapter {
public:
int test() {
return 0;
}
};

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

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com