我正在重新学习C++(意思是:请温柔对待我!:)。我有一个超类(Node
),其中包含一个必须在子类(TestNode
)中实现的抽象方法(step()
)。它可以编译而没有任何错误和警告,但链接它会产生以下结果:
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)
据我所知,我已经定义了“第一个非内联虚成员函数”(即
TestNode::step()
)。我仔细阅读了错误消息,阅读了这里的博客文章,并查看了许多其他SO帖子(Undefined symbols "vtable for ..." and "typeinfo for..."?, How to find undefined virtual functions of a classes, and c++ a missing vtable error),但是我感觉离启蒙还有很远的路要走。我错过了什么?以下是整个程序的内容。#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;
}