c ++对vtable的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4272432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ undefined reference to vtable
提问by Ben Fitzgerald
I'm learning C++. I'm trying to do an exercise where I define several implementations of a pure virtual class with a single function. I'm having trouble linking the class that uses these implementations.
我正在学习 C++。我正在尝试做一个练习,在该练习中我使用单个函数定义纯虚拟类的多个实现。我在链接使用这些实现的类时遇到问题。
==> BasicMath.h <==
#ifndef BASIC_MATH_H
#define BASIC_MATH_H
#include<string>
#include<vector>
class BasicMath { };
#endif // BASIC_MATH_H
==> Operation.h <==
#ifndef OPERATION
#define OPERATION
#include<string>
#include<vector>
class Operation {
public:
virtual void perform(std::vector<std::string> vec) = 0;
};
#endif // OPERATION
==> Sum.h <==
#ifndef SUM_H
#define SUM_H
#include "Operation.h"
class Sum: public Operation {
public:
void perform(std::vector<std::string> vec);
};
#endif // SUM_H
==> BasicMath.cpp <==
#ifndef BASIC_MATH_C
#define BASIC_MATH_C
#include <string>
#include <vector>
#include <iostream>
#include "BasicMath.h"
#include "Sum.h"
int main(int argc, char* argv[]) {
Sum op;
}
#endif // BASIC_MATH_C
==> Sum.cpp <==
#ifndef SUM_C
#define SUM_C
#include <vector>
#include <string>
#include <iostream>
#include "Sum.h"
void Sum::perform(std::vector<std::string> vec) {
using namespace std;
int total = 0;
cout << "Total: " << total << "\n";
};
#endif // SUM_C
Compilation:
汇编:
$ g++ -c Sum.cpp
$ g++ -o BasicMath BasicMath.cpp
/tmp/cc1VXjNl.o:BasicMath.cpp:(.text$_ZN3SumC1Ev[Sum::Sum()]+0x16): undefined reference to `vtable for Sum'
collect2: ld returned 1 exit status
I'm 95% sure I'm doing at least one foolish thing here - but my brain is refusing to tell me what.
我 95% 确定我在这里至少做了一件愚蠢的事情 - 但我的大脑拒绝告诉我什么。
I have see thisquestion but have not managed to fix my issue.
我已经看到这个问题,但没有设法解决我的问题。
采纳答案by Edward Strange
You're not including the Sum.o object file on your compile&link line (second g++ use).
您没有在编译和链接行(第二个 g++ 使用)中包含 Sum.o 目标文件。
回答by Till Kolditz
I Just encountered the same problem, but my problem was that I had not written the destructor codein my .cpp file.
我刚刚遇到了同样的问题,但我的问题是我没有在我的 .cpp 文件中编写析构函数代码。
class.h:
类.h:
class MyClass {
public:
MyClass();
virtual ~MyClass();
};
class.cpp:
类.cpp:
MyClass::MyClass() {}
It just gave me the vtable error message, and implementing the (empty) destructor solved the problem.
它只是给了我 vtable 错误消息,并且实现(空)析构函数解决了这个问题。
[Edit] Thus, the corrected class file looks like this:
[编辑] 因此,更正后的类文件如下所示:
MyClass::MyClass() {}
MyClass::~MyClass() {}
回答by Jerry Coffin
A couple of people have already pointed out the solution to the problem you've seen.
有几个人已经指出了您所看到的问题的解决方案。
I'll add something rather different. You only need header guards in your headers. You've included them in your source filesas well, where they really don'tmake sense. For example, I've commented out the lines you really don't need (or even want) in sum.cpp:
我会添加一些非常不同的东西。您只需要标题中的标题守卫。您也将它们包含在源文件中,但实际上它们没有意义。例如,我在 sum.cpp 中注释掉了你真正不需要(甚至不需要)的行:
//#ifndef SUM_C
//#define SUM_C
//
#include <vector>
#include <string>
#include <iostream>
#include "Sum.h"
void Sum::perform(std::vector<std::string> vec) {
using namespace std;
int total = 0;
cout << "Total: " << total << "\n";
};
//#endif // SUM_C
Just FWIW, instead of perform
, I'd use operator()
:
只是 FWIW,而不是perform
,我会使用operator()
:
class Operation {
public:
virtual void operator()(std::vector<std::string> vec) = 0;
};
and (obviously) that's also what you'd overload for Sum
. To use it, instead of something like:
并且(显然)这也是您要为Sum
. 要使用它,而不是像这样的东西:
Sum op;
op.perform();
You'd use something like:
你会使用类似的东西:
Sum op;
op();
This is particularly convenient when you combine your class with others (e.g., those in the standard library) that invoke operations like functions, whether they're really functions, or "functors" (classes like this, that overload operator()
so syntactically they can be used almost like functions).
当您将类与其他调用操作的类(例如,标准库中的类)结合使用时,这特别方便,无论它们是真正的函数,还是“函子”(像这样的类,在operator()
语法上重载它们都可以使用几乎就像函数一样)。
回答by doron
I normally encounter this error when I accidentally forget the =0
at the end of one of my functions in a pure virtual class.
当我不小心忘记=0
了纯虚拟类中的一个函数的末尾时,我通常会遇到此错误。
回答by EboMike
You're just compiling BasicMath.cpp without Sum.cpp - your linker has no idea about Sum.cpp. You'll need to compile them both together, i.e. Sum.cpp BasicMath.cpp
in one go, or you can compile the .cpp files independently and then create the executable by calling g++ with both .o files.
您只是在没有 Sum.cpp 的情况下编译 BasicMath.cpp - 您的链接器不知道 Sum.cpp。您需要将它们一起编译,即Sum.cpp BasicMath.cpp
一次性编译,或者您可以独立编译 .cpp 文件,然后通过使用两个 .o 文件调用 g++ 来创建可执行文件。