为什么我会被虚拟和具体的课程"Undefined symbols ... typeinfo ... vtable"?
Why am I getting "Undefined symbols ... typeinfo ... vtable" with a virtual and concrete class?
我正在重新学习C++(意思是:对我温柔一点!:)。我有一个具有抽象方法(step()
)的超类(Node
),该方法必须在子类(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)
据我所知,我已经定义了"第一个非内联虚拟成员函数"(即TestNode::step()
)。
我仔细阅读了错误消息,我阅读了这里的博客文章,并查看了许多其他SO文章(未定义的符号"vtable for…"answers"typeinfo for…"?,如何找到类的未定义虚拟函数,以及c++缺失的vtable错误),但我觉得离启蒙并不近。
我错过了什么?
这是整个程序。
#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 = %dn", count);
return *this;
}
} // namespace test
int main() {
return 0;
}
问题是您没有提供Node::step()
的实现。如果您真的希望Node没有步骤的实现,那么您应该使它成为一个纯虚拟函数Node::step(int count) = 0
,从而使Node成为一个抽象类(您不能直接实例化它)。否则,定义Node::步骤的实现。
据我所知,我已经定义了"第一个非内联虚拟成员函数"(即TestNode::step())。
您似乎混淆了定义和声明。基类中只有声明而没有定义,即实现。
您要么需要使它成为纯虚拟的,要么实现它,即使它只是一个空的{}。
class Node {
public:
virtual Node& step(int count);
};
快速解决方法可能是:
class Node {
public:
virtual Node& step(int count) = 0;
// ^^^ making it pure virtual
};
或:
class Node {
public:
virtual Node& step(int count) { };
// ^^^ empty implementation for now
};
相关文章:
- 与卡特琳娜一起卷曲C++失败并得到"Undefined symbols for architecture x86_64"
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 编译 OpenBLAS C++项目会产生错误"Undefined symbols for architecture x86_64"
- 使用多个文件时出现"Undefined symbols for architecture x86_64"错误
- 静态库链接 已定义符号的问题"Undefined symbols"
- 无法在 Xcode 中使用 Boost,不断收到"Undefined symbols for architecture"错误
- "Undefined Symbols"但定义和声明了函数,没有错别字
- 将某些功能与 main.cpp 分离时出现"undefined symbols for architecture"错误,该怎么办?
- XCode 错误"Undefined symbols for architecture x86_64" C++
- 了解"Undefined symbols for architecture"错误
- 在单独的 c++ 文件中初始化矢量时出现"undefined symbols for architecture"错误
- 有错误"Undefined symbols for architecture x86_64"和"ld: symbol(s) not found for architecture x86_64"
- "Undefined symbols for Architecture x86_64 "未使用模板,可能存在语法错误
- Xcode 6 上针对目标 C 的错误'Undefined symbols for architecture x86_64 '
- "Undefined symbols for architecture x86_64"尝试包含另一个文件中的功能
- Visual Studio "object file does not define any previously undefined symbols"
- XCode 构建失败"undefined Symbols for architecture x86_64"
- 为什么我会被虚拟和具体的课程"Undefined symbols ... typeinfo ... vtable"?
- C++:使用"std"时出错"Undefined symbols for architecture x86_64"
- osx 上的编译错误"Undefined symbols"