Undefined reference to a derived class

EDIT: C ++ link undefined link to `vtable

I am trying to make a project to inherit, and I get this error:

/tmp/ccw1aT69.o: In function `main': main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)' /tmp/ccw1aT69.o: In function `Derived::~Derived()': main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x13): undefined reference to `vtable for Derived' main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x1f): undefined reference to `Base::~Base()' collect2: ld returned 1 exit status 

This is my code:

main.cpp:

 #include <iostream> #include "Base.h" #include "Derived.h" int main() { Derived intList(25); } 

base.h:

 #ifndef BASE_H #define BASE_H class Base { public: ... Base (const Base& otherList); virtual ~Base(); protected: int *list; int length; int maxSize; }; #endif 

Base.cpp:

 #include "Base.h" #include <iostream> using namespace std; ...definitions of my members... Base::Base (int size) { //stuff } Base::~Base() { delete [] list; } Base::Base (const Base& otherList) { //stuff } 

Derived.h:

 #ifndef DERIVED_H #define DERIVED_H #include "Base.h" class Derived: public Base { public: ... Derived (int size = 100); ~Derived(); //THIS LINE ADDED AFTER FIRST ANSWER }; #endif 

Derived.cpp:

 #include "Derived.h" #include <iostream> using namespace std; Derived::Derived (int size) :Base(size){ } 

What causes this error? It looks like I can't call the constructor, but I'm fine.

EDIT: I tried the first solution. Error:

 /tmp/ccA4XA0B.o: In function `main': main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)' main.cpp:(.text+0x21): undefined reference to `Derived::~Derived()' collect2: ld returned 1 exit status 
+4
source share
3 answers

You declared a virtual destructor in Base , but you never define it. It must be defined in Derived (as well as in Base , because it is not a pure virtual function), because it will be called as soon as main completed. You must have:

 class Base { public: // ... virtual ~Base(); }; Base::~Base() {} class Derived : public Base { public: // ... ~Derived(); }; Derived::~Derived() { /* whatever */ } 

This is the cause of at least one of your mistakes. I don't know if this red herring is or not, but it looks like:

/tmp/ccw1aT69.o: In the main': main.cpp:(.text+0x15): undefined reference to function main': main.cpp:(.text+0x15): undefined reference to Derivatives :: Derivatives (int)'

You define Derived::Derived(int) , so it’s hard to imagine that this is a real mistake. Define your destructor and see if it leaves.

+7
source

Ok, just for clarity, and since I can't put formatted code in anything but an answer. You do NOT need to provide a destructor in a derived class just because your base class has a virtual dtor or pure methods. The following is about as simple as I can demonstrate this under three different conditions of construction / destruction. the output will be indicated after the code. Hope this at least helps @ Jeff. I tested this under VS2005 / 2008/2010 and ancient gcc 4.1.2, so it's better to be right.

 #include <iostream> class Base { public: Base() { std::cout << "Base()" << std::endl; }; virtual void call_me() = 0; virtual ~Base() { std::cout << "~Base()" << std::endl << std::endl; }; }; class Derived : public Base { public: Derived(int i=1) { std::cout << "Derived(" << i << ")" << std::endl; } // Base::call_me requirements. void call_me() { std::cout << "call_me()" << std::endl; } }; int main(int argc, char* argv[]) { // use derived class pointer type Derived* pDerived = new Derived(); pDerived->call_me(); delete pDerived; // use base class pointer type Base* pBase = new Derived(2); pBase->call_me(); delete pBase; // scope based { Derived obj(3); obj.call_me(); } return 0; } 

The output for this is:

 Base() Derived(1) call_me() ~Base() Base() Derived(2) call_me() ~Base() Base() Derived(3) call_me() ~Base() 
+3
source
 main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)' 

This error message does not report the undefined link to the class, as your title says. Rather, he complains about an undefined reference to a constructor that accepts an int parameter. From my cursory glance at your code, you only declared this constructor and did not define it. Add the definition to your .cpp file and you must resolve this error.

In addition, it is common practice to place constructor declarations before declarations of all member functions. At first, I skipped these statements because they were not there where I expected. I highly recommend you do this to avoid future misunderstandings when you ask questions here.

0
source

Source: https://habr.com/ru/post/1433068/


All Articles