开发者

Why does compiler complain when I try to convert to a base-class?

BaseClass.h

class BaseClass
{
 ...
};

SubClass.h

#include "BaseClass.h"
class SubClass : public BaseClass
{
 ...
};

MyApp.h

class BaseClass;
class SubClass;
class MyApp
{
 SubClass *pObject;

 BaseClass *getObject()
 {
  return pObject;
 }
};

I get a compiler error: err开发者_如何学Goor C2440: 'return' : cannot convert from 'SubClass *' to 'BaseClass *'

Why doesn't it work, surely you can automatically convert to a base-class without any casting?


Only post code which you have tested to exhibit the described behaviour.

Your code, with the "pOject" typo and the "..." parts removed, compiles just fine.

Edit after OP completely reworked the question:

The code in MyApp.h doesn't know that SubClass is a subclass of BaseClass, because you did not include the headers. All MyApp.h sees are forward declarations of the classes, which allows to handle pointers but doesn't allow to cast.


In "MyApp.h", you only have forward declarations of the classes, so it's not known that one derives from the other. You will need to include the "SubClass.h" before the body of getObject(); either include it from "MyApp.h", or move the body of getObject() into a source file and include it from there.


SubClass is not a BaseClass subclass in your example.

Apart from that your example compiles properly in my g++.

With the last edit you have, you are forward declaring 2 classes, and the compiler will not know that one is subclass of the other that way so no conversion is available.


MyApp.h doesn't tell the compiler that SubClass inherits from BaseClass - you should probably include baseclass.h and subclass.h in your myapp.h file as long as you have getObject() inlined there.


Edit: In your MyApp.h you should #include "SubClass.h". The forward declaration alone that you have has no info about one being a subclass of the other.


About your first post's problem: (Trying to store a BaseClass inside a SubClass)

You have had it mixed up. (Your edit fixes the problem)

A BaseClass pointer or reference can hold a SubClass memory address. Not the other way around.

A subclass is a base class and that's why all of a base class's methods and properties will apply to it.

If you had it the other way, you could store a base class address in a subclass pointer or reference, then that's a problem because you can call some method that a base class object doesn't support.


Because your classes are forward declared, therefore while parsing your header file the compiler has no idea that the two types are related.

You should move this method to the source file MyApp.cpp in which you will include SubClass.h.


The code works for me on g++ -pedantic (after changing pOject to pObject). The error must be somewhere else.

Your updated code contains the error:

class BaseClass;
class SubClass;

How is the compiler to know that these two classes are related? From the compiler’s point of view, the classes have no relationship whatsoever, so they cannot be interchanged.

Remove the forward declarations and instead #include "SubClass.h" to make the code work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜