Problem with inheritance

Hello
I'm beginner in inheritance and I'm trying to apply it with a simple example:
I have a main class - automobile (header and cpp file )
1
2
3
4
5
6
7
8
9
10
#ifndef AUTOMOBILE_H_INCLUDED
#define AUTOMOBILE_H_INCLUDED
using namespace std;
class automobile {
 public:
	automobile(unsigned int speed=0);
 protected:
    unsigned int itsspeed;
};
#endif // AUTOMOBILE_H_INCLUDED 


its cpp execution file automobile.cpp :
1
2
3
4
5
6
#include "automobile.h"
#include <iostream>
automobile::automobile(unsigned int speed)
{
	itsspeed = speed;
}

a child class (van) with the header :
1
2
3
4
5
6
7
8
#ifndef VAN_H_INCLUDED
#define VAN_H_INCLUDED
#include "automobile.h"
class van : public automobile {
 public:
	van(unsigned int speed=0);
};
#endif // VAN_H_INCLUDED 

its cpp execution file van.cpp:
1
2
3
4
5
6
#include "van.h"
#include <iostream>
van::van(unsigned int speed)
	: automobile(speed)
{
}

and my main function is as follow :
1
2
3
4
5
6
7
8
#include "van.h"
#include "automobile.h"
#include <iostream>
int main()
{
    van a;
	return 0;
}


but always i have the same error :

undefined reference to 'van::van()(unsigned int)'


this is making me sick, help please
I think your error might be caused by the discrepancy between the lines:
van(unsigned int speed=0); and van::van(unsigned int speed)
In the first occurrence of the van constructor, you set a default parameter and in the second occurrence you made it, well, not so default.
i dont think so , because even if I modify it to van(unsigned int speed) i still have the same error
Try modifying the other constructor to unsigned int speed=0
or van a; to van a(80);
Last edited on
Giving the implementation of the destructor a default value is an error, only forward declarations can have those - otherwise you could get weird times where you have specified different default values...

The most likely cause I can see for your problem is that you aren't compiling / linking to your van.cpp file, hence the 'undefined reference' error.
NT3 is right, your code should work fine.
I feel kind of dumb right now...
What IDE do you use? You are getting a linker error. Try to see if the files are in the project. If they are, just try saving the project and recompiling
Topic archived. No new replies allowed.