Problem with derived classes

Hi all,

I have a base class "Base" and a derived class from the Base called "Derived".

Can I find out how come the Derived class can't be properly 'connected' to the Base class when I separate the classes into different .cpp file. And how can I rectify the problem.

For example if I have only one .cpp file with everything in it, there's no problem and the program will execute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Main.cpp one file -success
class Base
{
public:
	Base(){}
};

class Derived : public Base
{
public:
	Derived(){}
};

int main (void)
{
	Base* d;
	d = new Derived();
	return 0;
}


But if I split it into three files, there's a problem:

1
2
3
4
5
6
7
8
9
10
//Main.cpp ERROR
#include "Base.cpp"
#include "Derived.cpp"

int main (void)
{
	Base* d;
	d = new Derived();
	return 0;
}


1
2
3
4
5
6
//base.cpp ERROR
class Base
{
public:
	Base(){}
};


1
2
3
4
5
6
7
8
//Derived.cpp
#include "Base.cpp"

class Derived : public Base
{
public:
	Derived(){}
};


This is the error message from the compiler:

1
2
3
4
5
6
7
1
1>  Main.cpp
1>c:\users\asd\documents\visual studio 2010\projects\trial100912\trial100912\base.cpp(2): error C2011: 'Base' : 'class' type redefinition
1>          c:\users\asd\documents\visual studio 2010\projects\trial100912\trial100912\base.cpp(2) : see declaration of 'Base'
1>c:\users\asd\documents\visual studio 2010\projects\trial100912\trial100912\derived.cpp(4): error C2504: 'Base' : base class undefined
1>c:\users\asd\documents\visual studio 2010\projects\trial100912\trial100912\main.cpp(7): error C2440: '=' : cannot convert from 'Derived *' to 'Base *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I tried using header formats as well (.h files) but the problem still persisted. Help?
The compiler is pointing you toward the problem: You are including Base.cpp in both main.cpp and Derived.cpp.

rename Base.cpp to Base.h, and rename Derived.cpp to Derived.h. Put include sentinels in both headers like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#ifndef BASE_H

#define BASE_H

class Base
{
public:
    Base() {}
}
;

#endif


Of course, use #ifndef DERIVE_H in Derived.h. Hope this helps!
Okay, thanks a lot for helping kooth, it works!
The problem is you're including base.cpp multiple times.

Once in main.cpp and again in in derived.cpp, so when you compile main.cpp you get the following:
main.cpp
-> include base.cpp
-> include derived.cpp
----> include base.cpp (duplicate)

base.cpp and derived.cpp should both be .h files, otherwise you're going to get linker errors because of multiply defined classes. You want the compiler to compile only main.cpp.

You can avoid the multiple declarations by bracketing the header files

1
2
3
4
5
6
7
8
9
//base.h
#ifndef _BASE_H_
#define _BASE_H_
class Base
{
public:
	Base(){}
}; 
#endif 


1
2
3
4
5
6
7
8
9
10
11
// derived.h
#ifndef _DERIVED_H_
#define _DERIVED_H
#include "Base.h"

class Derived : public Base
{
public:
	Derived(){}
}; 
#endif 


Note. YOu could have avoided the problem by not including base.h inside derived.h since you've included both in main.cpp, however, it's a good practice to do so.






Topic archived. No new replies allowed.