Problem with abstract classes and inheritance

I am having a problem with a set of inherited classes and I cannot figure out why. Here is the relevant code (with class names changed). (Each separate code file in separate code section.)

1
2
3
4
5
class MyAbstractBaseClass
{
public:
	virtual unsigned int id() = 0;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// MyDerivedBaseClasses.h
class MyDerivedAbstractClass : MyAbstractBaseClass
{
	// does nothing with the id() function
};

...
// code between includes a MyPersistentBaseClass irrelevant to problem
...

// this class is for objects that will only be around a short time.
class MyVolatileBaseClass : MyDerivedAbstractClass
{
public:
	virtual unsigned int id();

private:
	unsigned int mId;
};


1
2
3
4
5
// MyDerivedBaseClasses.cpp
#include "MyDerivedBaseClasses.h"
...

unsigned int MyVolatileBaseClass::id() { return mId; } 


1
2
3
4
5
6
7
// each derived volatile to be used in code has its own header and cpp file

// I have several derived volatile types that all present the same problem...
class DerivedVolatileClassType1 : MyVolatileBaseClass 
{
	// relies on base class id() function so no override here...
}


1
2
3
4
5
6
7
// code that gives error...
void main()
{
	// it doesn't matter which derived volatile type I use here... 
	// all give same error... so replace "Type1" with "Type2", "Type3", etc...
	DerivedVolatileClassType1* instance = new DerivedVolatileClassType1(); // gives error
}


The line with "// gives error" always gives the same error:

"Object of abstract type "DerivedVolatileClassType1" (Type2... etc...) not allowed. DerivedVolatileClassType1::id has no overrider"
Last edited on
Well, you have a variable id and a function id. The compiler will have a hard time to distinguish...

Further more:

DerivedVolatileClassType1 instance = new DerivedVolatileClassType1(); // A * needed befor instance
Sorry... the actual code is correct. My example is not. I have fixed the code in the example. There is no confusion in the real code as my field is mID and the function is id(). I also used a pointer rather in the code.
Last edited on
It's easier to help if you make sure the code that you post actually gives the error message that you want help fixing.

You should use public inheritance, but I guess that is just another mistake in your example...

The error message says that DerivedVolatileClassType1 is an abstract class. Make sure all pure virtual functions have been overridden.
Last edited on
You should use public inheritance, but I guess that is just another mistake in your example...


Thanks for reminding me... I still forget to do that (I am still trying to break c# habits from too many years using lol.). Still... while it was an issue with one class, it wasn't with another and it is not the source of the problem (though it makes sense that it could have been). I still have the problem after fixing that issue.

LOL. I was creating a version that was as near enough to an exact copy as I could make when I found the problem... the id() in my abstract class had the following signature (verbatim):

virtual unsigned int id() const = 0;

The one in my derived class ("MyVolatileBaseClass") had this signature:

virtual unsigned int id();

Um.... missing the const there. LOL. Details, details... now I feel like an idiot.

Last edited on
Topic archived. No new replies allowed.