Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined reference to a derived class
    text
    copied!<p>EDIT: RELATED TO <a href="https://stackoverflow.com/questions/12327679/c-undefined-reference-to-vtable/12327728#12327728">c++ undefined reference to `vtable</a></p> <p>I am trying to do a project on inheritance and I'm getting this error:</p> <pre><code>/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 </code></pre> <p>This is my code:</p> <p>main.cpp:</p> <pre><code>#include &lt;iostream&gt; #include "Base.h" #include "Derived.h" int main() { Derived intList(25); } </code></pre> <p>base.h:</p> <pre><code>#ifndef BASE_H #define BASE_H class Base { public: ... Base (const Base&amp; otherList); virtual ~Base(); protected: int *list; int length; int maxSize; }; #endif </code></pre> <p>Base.cpp:</p> <pre><code>#include "Base.h" #include &lt;iostream&gt; using namespace std; ...definitions of my members... Base::Base (int size) { //stuff } Base::~Base() { delete [] list; } Base::Base (const Base&amp; otherList) { //stuff } </code></pre> <p>Derived.h:</p> <pre><code>#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 </code></pre> <p>Derived.cpp: </p> <pre><code>#include "Derived.h" #include &lt;iostream&gt; using namespace std; Derived::Derived (int size) :Base(size){ } </code></pre> <p>What causes this error? It looks like I can't call the constructor, but it looks fine to me.</p> <p>EDIT: I tried the first solution. Error now:</p> <pre><code>/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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload