c ++对vtable的未定义引用

Ben*_*ald 16 c++ g++

我正在学习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
Run Code Online (Sandbox Code Playgroud)

汇编:

$ 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
Run Code Online (Sandbox Code Playgroud)

我95%肯定我在这里做了至少一件愚蠢的事 - 但我的大脑拒绝告诉我什么.

我看到了这个问题,但没有设法解决我的问题.

Til*_*itz 26

我刚遇到同样的问题,但我的问题是我没有在我的.cpp文件中编写析构函数代码.

class.h:

class MyClass {
public:
    MyClass();
    virtual ~MyClass();
};
Run Code Online (Sandbox Code Playgroud)

class.cpp:

MyClass::MyClass() {}
Run Code Online (Sandbox Code Playgroud)

它只是给了我vtable错误信息,实现(空)析构函数解决了这个问题.

[编辑]因此,更正后的类文件如下所示:

MyClass::MyClass() {}
MyClass::~MyClass() {}
Run Code Online (Sandbox Code Playgroud)

  • 显然,这很常见.+1 ......这有助于我解决同样的问题. (4认同)

Edw*_*nge 17

您没有在编译和链接行中包含Sum.o对象文件(第二个g ++使用).


Cir*_*四事件 7

如果您忘记= 0for pure virtual 函数,也会发生该错误

错误:

class Base {
    public:
        virtual void f();
};

class Derived : public Base {
    public:
        virtual void f() {}
};

int main() {
    Derived d;
    Base *b = &d;
}
Run Code Online (Sandbox Code Playgroud)

没有错误:

class Base {
    public:
        virtual void f() = 0;
};
Run Code Online (Sandbox Code Playgroud)

这是因为没有= 0,C++ 不知道它是纯虚函数,将其视为声明,期待稍后的定义。

g++5.2.1 上测试。


Jer*_*fin 6

有几个人已经指出了你所看到的问题的解决方案.

我会添加一些不同的东西.您只需要标题中的标题保护.您已经将它们包含在源文件中,它们实际上没有任何意义.例如,我在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
Run Code Online (Sandbox Code Playgroud)

只是FWIW,而不是perform,我会使用operator():

class Operation {
 public:
  virtual void operator()(std::vector<std::string> vec) = 0;
};
Run Code Online (Sandbox Code Playgroud)

(显然)这也是你超载的原因Sum.要使用它,而不是像:

Sum op;
op.perform();
Run Code Online (Sandbox Code Playgroud)

你会使用类似的东西:

Sum op;
op();
Run Code Online (Sandbox Code Playgroud)

当你将你的类与其他人(例如标准库中的那些)结合起来调用类似函数的操作,它们是真正的函数,还是"函子"(像这样的类,这样的重载operator(),语法上它们可以被使用)时,这是特别方便的几乎像功能).