views:

157

answers:

7

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: error 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?

+2  A: 

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.

Arkaitz Jimenez
typo, fixed now.
John
+2  A: 

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.

Brian R. Bondy
I don't think so. A subclass adds extra data to a base class. A subclass IS A base-class...
John
@John: See my edit I explain more.
Brian R. Bondy
@John: I added more info to match your edit.
Brian R. Bondy
A: 

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.

Konrad Rudolph
are all classes in one file?
John
@John: Yes. But that’s irrelevant.
Konrad Rudolph
It's not irrelevant. if he includes myapp.h in some other files the compiler won't know that SubClass inherits from BaseClass within that translation unit.
nos
@nos: I wrote that before seeing his update. It *would be* irrelevant if the files contained the same code, with appropriate `#include` s.
Konrad Rudolph
+5  A: 

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.

DevSolar
even respecting the file structure?
John
No. That file structure was not part of the question when I answered it, hence my answer doesn't take it into account. Moral: Think about your question before you post, otherwise the answers will suck.
DevSolar
Answer updated.
DevSolar
+1 For the lesson in etiquette; that and for an otherwise under-appreciated answer.
andand
Your -1 was for "Only post code which you have tested to exhibit the described behaviour." I'm tired of people saying that with no thought about how hard it is to strip a big class down.
John
Removed the down-vote now the answer is helpful. But you jumped to conclusions. Even if I had posted real code, I might not have thought to specify which files things were in, since that turned out to be the problem.
John
I absolutely stand to that comment. Being able to reduce a problem to the minimum code necessary to reproduce the error is a basic skill for programmers, however woefully absent from many in the business today. In 9 out of 10 cases, this would already solve your problem because you realize your mistake yourself. If you're tired of people telling you to do your homework, well, perhaps you start doing it instead of complaining about it. I've got ten years' experience as a maintenance coder, 8 of those with C++. You think I don't know how hard it is? Think again.
DevSolar
Yes, but I'd have expected an expert C++ coder to see my initial post and respond "the only reason this wouldn't work is if you had forward-declared classes rather than #included your .h files". In fact, I'm considering if I can use this as a C++ test question in future!
John
Ah! Now it's my fault for not being psychic. If you ask that as a "C++" test question, all you'll be able to test for is if the candidate is willing to work in the same company as you. I wouldn't be.
DevSolar
I posted code without saying what files it's in and you _assumed_ that means it's all in one file. That's _not_ the normal way of doing things in C++ if you plan on an organised codebase. If you were as smart as you seem to think (obviously not the case) your response would have asked how the classes were organised, rather than a dumb 'it works' response.
John
An experienced C++ developer _should_ know the differences between including and forward-declaring classes to avoid problems such as this one. Therefore it's a fair C++ test question. _You_ might think it needs psychic ability but your response to such a question demonstrates not only your narrow-minded approach to problem solving, but that your opinion of yourself exceeds your actual skill. As such, it's a great question if it weeds out applicants like you.
John
You're killing me, really. :-D The one who'd be weeded out by your "great" test question would be you, because you're the one who made the mistake and I was the one pointing it out.I'm out of here. Popcorn's empty.
DevSolar
+4  A: 

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.

Mike Seymour
Damnit, I hate how C++ does #includes and forward declarations!!!
John
I hate it how people blame the language for their lack of understanding.
DevSolar
Oh grow up, for goodness' sake
John
+2  A: 

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.

nos
+2  A: 

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.

Matthieu M.