22

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.

==> 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.

I have see this question but have not managed to fix my issue.

3
  • 1
    Side note: You definitely want to pass an std::vector by reference, not by value.
    – EboMike
    Nov 24, 2010 at 23:23
  • Side note 2: Classes with virtual functions should have a virtual destructor.
    – EboMike
    Nov 24, 2010 at 23:24
  • I have added the g++ tag, as your problem is in how to get the compiler (more precisely linker) to link object files, and that is compiler dependent. Nov 25, 2010 at 0:03

7 Answers 7

31

I Just encountered the same problem, but my problem was that I had not written the destructor code in my .cpp file.

class.h:

class MyClass {
public:
    MyClass();
    virtual ~MyClass();
};

class.cpp:

MyClass::MyClass() {}

It just gave me the vtable error message, and implementing the (empty) destructor solved the problem.

[Edit] Thus, the corrected class file looks like this:

MyClass::MyClass() {}
MyClass::~MyClass() {}
3
  • 4
    Apparently, this can be common enough. +1 ... this helped me resolve the same issue.
    – IAbstract
    Apr 7, 2015 at 21:23
  • The second code block isn't "implementing the (empty) destructor" as you say, that's implementing the constructor.
    – DBedrenko
    Mar 29, 2016 at 14:52
  • 1
    The second code block intentionally displayed the state which produced the error. I thought the sentence afterwards would be suffice for the solution but I added the "corrected" contents. Mar 31, 2016 at 10:21
20

You're not including the Sum.o object file on your compile&link line (second g++ use).

2
  • ok that was excruciatingly easy. Thanks Noah. Take the other points about pass by ref etc. I'll work on that next now I'm past this error. g++ manual goes on the reading list! Nov 24, 2010 at 23:38
  • Correct, I was not compiling the cpp file of an included class.
    – zpon
    Mar 5, 2018 at 13:53
10

That error also happens if you forget the = 0 for pure virtual functions

Error:

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

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

int main() {
    Derived d;
    Base *b = &d;
    (void)b;
}

No error:

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

This is because without the = 0, C++ does not know that it is a pure virtual function, treats it as a declaration, expecting a later definition.

Tested on g++ 5.2.1.

Tested as of GCC 11.2.0, the error message changed to:

undefined reference to `typeinfo for Base'

command:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp
1
  • 1
    guy you save my life XD
    – vivi
    Oct 5, 2017 at 21:39
6

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 files as well, where they really don't make sense. For example, I've commented out the lines you really don't need (or even want) in 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():

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 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).

1
  • Hi Jerry - I added the guards in my source after reviewing some of the ACE code, which had this style cs.wustl.edu/~schmidt/ACE.html. Perhaps this is required for some frameworks or is just now defunct? Dec 2, 2010 at 10:09
2

I normally encounter this error when I accidentally forget the =0 at the end of one of my functions in a pure virtual class.

4
  • That has nothing to do with it. =0 means that your class will be abstract. I guess the problem is that you forgot to write a function body.
    – EboMike
    Nov 25, 2010 at 0:05
  • Thanks, this answer helped me.
    – Alcamtar
    Sep 24, 2017 at 10:58
  • @EboMike The body is not required when you use ... = 0. So it would solve some of the issues (not for a destructor, though). May 15, 2021 at 21:41
  • Why that works: the = 0 ensures the compiler/linker doesn't try to find that member function definition (implementation) for that virtual class -- which doesn't exist when you want that member function to be virtual ;-) May 24 at 8:49
1

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.

0
1

I have met the problem same as yours , and I solved this problem by adding three lines in the CMakeLists.txt , that is:

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
2
  • Hi, please keep in mind that, as the cmake manual shows, these are very specific to cmake and QT projects ;-) May 28, 2021 at 10:03
  • Thanks, I were so troubled without knowing my problem also with MOC. for some reason my header file wasn't targeted by CMAKE_AUTOMOC so I had to use #include "moc_<header_base>.cpp" instead of #include "<header_base>.h". hope it helps if someone face the same problem. Mar 12 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.