Undefined reference to a derived class

EDIT: RELATED TO c++ undefined reference to `vtable

I am trying to do a project on inheritance and I'm getting 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 it looks fine to me.

EDIT: I tried the first solution. Error now:

/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

Answers


You have declared a virtual destructor in Base, yet you never define it. It needs to be defined in Derived (as well as in Base because it is not a pure virtual function) because it will be called once main exits. You should have:

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

Base::~Base() {}

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

Derived::~Derived() { /* whatever */ }

That is the cause of at least one of your errors. I don't know if this one is a red herring or not, but it seems to be:

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

You define Derived::Derived(int), so it's hard to imagine that is a real error. Define your destructor and see if it goes away.


Need Your Help

What is the difference between these two codes?

javascript extjs

I am new to ExtJs, just stepped into some basic things and found that its very hard to get started as a beginner.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.