我正在重新学习 C++(意思是:对我温柔点!:)。我有一个超类(Node
)与抽象方法(step()
)必须在子类(TestNode
)。它编译时没有错误,也没有任何警告,但链接它会导致:
bash-3.2$ g++ -Wall -o ./bin/t1 src/t1.cpp
Undefined symbols for architecture x86_64:
"typeinfo for test::Node", referenced from:
typeinfo for test::TestNode in t1-9f6e93.o
"vtable for test::Node", referenced from:
test::Node::Node() in t1-9f6e93.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
据我所知,我have定义了“第一个非内联虚拟成员函数”(即TestNode::step()
).
我仔细阅读了错误消息,我阅读了博客文章here http://www.educatedguesswork.org/2011/09/undefined_symbol_vtable_what_t.html并查看了许多其他 SO 帖子(未定义的符号“vtable for ...”和“typeinfo for...”? https://stackoverflow.com/q/1693634/558639, 如何查找类中未定义的虚函数 https://stackoverflow.com/q/3658072/558639, and C++ 缺少 vtable 错误 https://stackoverflow.com/q/15265106/558639),但我觉得离启蒙还没有更近。
我缺少什么?
这是整个程序。
#include <stdio.h>
namespace test {
class Node {
public:
virtual Node& step(int count);
};
class TestNode : public Node {
public:
TestNode();
~TestNode();
TestNode& step(int count);
};
TestNode::TestNode() { }
TestNode::~TestNode() { }
TestNode& TestNode::step(int count) {
printf("count = %d\n", count);
return *this;
}
} // namespace test
int main() {
return 0;
}
问题是你没有提供实现Node::step()
。如果你真的希望 Node 没有步骤的实现,那么你应该将其设为纯虚函数Node::step(int count) = 0
,从而使 Node 成为一个抽象类(不能直接实例化它)。否则,定义 Node::step 的实现。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)