1

I'm trying to write a small C++ inheritance program, that will contain virtual functions, headers, and 3 class, A, B:A, C:B, after compiling succeeds, the linker fails in the derived of a derived class, C.o, stating

relocation against `_ZTV26C' in read-only section `.text'

and

undefined reference to `vtable for C'

can anyone see what am i missing?

A.h

#ifndef A_H_
#define A_H_
#include <string>
#include <iostream>
using namespace std;

class A {
protected:
    string name;
public:
    A(string name);
    virtual ~A();
    virtual void justATest(){}
    void justBecause();
    virtual bool derivedTest(){}
};

#endif /* A_H_ */

A.cpp

#include "A.h"

A::A(string name) {this->name.assign(name);}

A::~A() {}

void A::justBecause(){}

B.h

#ifndef B_H_
#define B_H_
#include "A.h"
#include <string>

class B : public A{
public:
    B(string name):A(name){}
    virtual ~B(){}
    bool derivedTest();
};

#endif /* B_H_ */

B.cpp

#include "B.h"
bool B::derivedTest()
{
    return true;
}

C.h

#ifndef C_H_
#define C_H_
#include "B.h"

class C : public B{
public:
    C(string name) :B(name){}
    virtual ~C(){}
    void justATest();
};

#endif /* C_H_ */

C.cpp

#include "C.h"
void C::justATest()
{
    cout<< this->name;
}

main.cpp

#include "C.h"
int main(int argc, char* argv[]) {
    C* c = new C("str");
return 0;
}

the exact make command, and the error message: make all Building target: demo_proj Invoking: GCC C++ Linker g++ -o "ass5_demo" ./A.o ./B.o ./C.o ./main.o /usr/bin/ld: ./main.o: warning: relocation against _ZTV1C' in read-only section .text._ZN1CC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN1CC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]' /usr/bin/ld: ./main.o: in function C::C(std::__cxx11::basic_string<char, std::char_traits, std::allocator >)': /home/some_user/eclipse-workspace/demo_proj/../C.h:14: undefined reference to vtable for C' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE

MichaelK
  • 189
  • 2
  • 11
  • 2
    one of your future problems: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). It has potential for most confusing bugs everywhere, but in a header you should never use it – 463035818_is_not_an_ai Jan 21 '23 at 19:41
  • `Undefined reference to vtable` means that the linker couldn't find a virtual function to link against. In my experience this is usually a `virtual` door which was defined in the header, but not implemented. – SimonC Jan 21 '23 at 19:41
  • 4
    how did you compile and link? – 463035818_is_not_an_ai Jan 21 '23 at 19:42
  • @SimonC I suppose you mean "declared but not implemented / defined". – 463035818_is_not_an_ai Jan 21 '23 at 19:45
  • Yes. I constantly mix up the exact wording of defined and declared; I use them interchangeably. My bad. – SimonC Jan 21 '23 at 19:48
  • "how did you compile and link?" using g++ in eclipse, linux os – MichaelK Jan 21 '23 at 19:52
  • 1
    "using g++ in eclipse" says nothing at all. Please show the exact actual compilation command that Eclipse ran for you. By the way Eclipse is old, clunky, buggy and not very user-friendly. I recommend against using an IDE while learning. Edit with a simple text editor and compile on the command line. (But this is just my personal recommendation) – n. m. could be an AI Jan 21 '23 at 20:04
  • make all Building target: ass5_demo Invoking: GCC C++ Linker g++ -o "ass5_demo" ./A.o ./B.o ./C.o ./main.o /usr/bin/ld: ./main.o: warning: relocation against `_ZTV1C' in read-only section `.text._ZN1CC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN1CC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]' /usr/bin/ld: ./main.o: in function `C::C(std::__cxx11::basic_string, std::allocator >)': /home/some_user/eclipse-workspace/demo_proj/../C.h:14: undefined reference to `vtable for C' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE – MichaelK Jan 21 '23 at 20:21
  • 1
    Please [edit] the question and add this info there, *formatted as code*. It is unreadable as a comment. – n. m. could be an AI Jan 21 '23 at 22:44
  • Does this answer your question? [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) – JaMiT Jan 22 '23 at 10:01

1 Answers1

1

After many hours of debugging, and countless tries, with the same error message i found the answer. This is C++ linker crooked way of telling me there is an unimplemented method in one of my classes. adding to c.h

bool derivedTest() override;

and to c.cpp

bool C::derivedTest(){return false;}

p.s another nice way of finding the problem is adding override after each message in the header file, the compiler is much clearer

MichaelK
  • 189
  • 2
  • 11
  • I'm sorry I didn't see your question earlier, would've spared you the agony. I knew the answer as soon as I read the question title. It happens to me all the time, so I already know what this cryptic error message means :) – eltomito Jan 21 '23 at 21:46
  • there must be somthing you arent showing us or something else you do in addition to adding this method. In the posted code `A::derivedTest` already has an implementation. It should be all fine. – 463035818_is_not_an_ai Jan 21 '23 at 22:01
  • The answer is incorrect. There are no unimplemented methods. `C` does not *need* `derivedTest` to function. You *may* add it, but is not a diagnosable error not to have it. – n. m. could be an AI Jan 21 '23 at 22:49
  • nonetheless, this is what fixed my problem – MichaelK Jan 22 '23 at 07:06
  • Instead of defining a function you don't need, try moving the definition of the destructor to the source file (see the example at the end of my answer [here](https://stackoverflow.com/a/57504289/)). – JaMiT Jan 22 '23 at 10:09