Sorry if this is obvious (it's a bit diff. from java). I have two classes declared in the same file and both refer to each other in their code. So the one that's declared first won't compile. How do I fix this?
Sorry if this is obvious (it's a bit diff. from java). I have two classes declared in the same file and both refer to each other in their code. So the one that's declared first won't compile. How do I fix this?
You need to forward declare one of the classes, e.g.:
--Code:class B; class A { ... B *b; }; class B { ... };
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You need to make a forward declaration of the second class. That way the first will know about it. A bit like a function prototype.
Note that again, this isn't Java. With a forward declaration, you are only allowed to refer to the second_class through a pointer or reference. And if you have inline functions inside first_class, they will not be able to access the members of second_class since it has not been defined yet.Code:class second_class; class first_class { ... }; class second_class { ... };
EDIT: Hah! We all posted at the same exact MINUTE.
Thanks. Does this change if one class needs to call the constructor of the other and the other class needs to call a method from the first class? Is this possible?
Example:
Code:class Loader { public: static void load(std::string name) { //Do some loading here ....elided... if ( problemOccurred ) throw( new MYException( name ) ); } }; class MyException : std::exception { public: MyException(std::string name) { ...elided... } virtual const char* std::exception::what() const throw () { //Do some loading here Loader.load( name ); ....elided.... } };
This PARTICULAR example is okay (I think), but in general, using inline functions in classes which refer to each other isn't going to work. Try to get into the habit of writing your functions out-of-line, instead of in the class definition.
EDIT: Also, having an exception calling the Loader is truly weird. I see a great potential for an infinite loop of exception throwing, here. You declare what() as throw(), and yet that is a lie, because it calls Loader::load, which can throw.
I doubt you've actually tried compiling that. There are still Java-isms floating around. It's Loader::load, not Loader.load.
Last edited by brewbuck; 04-11-2008 at 09:56 AM.
Don't worry, there's no chance of looping (the other constructors/methods work completely differently).
Question: I was originally doing the first class as just functions outside a class but that wasn't working since it needed knowledge of the constructor of the exception class (which needed knowledge of the functions). So how would I do that?
It should work to write the functions outside of the class, as long as you have declared the existance of both classes.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Honestly I kind of don't want to help you take this perversion any farther than it already is I don't agree that it can't infinitely loop, I think it clearly can. And the throw() clause is still a lie. And actually doing "stuff" inside the what() method of an exception is not a good idea.
But if you can cough up an example of how you were trying to do it before (a real piece of code, not a paraphrase), I'll see what I can tell you.
OK, I figured it out. I do a forward declaration of the function and then put the actual definition after the class. Nice!
Still, it is weird to program where classes have so much trouble knowing about each other like that.
If what() lets an exception escape, the program either aborts because an exception was thrown during unwind, or it eventually runs out of try blocks and aborts anyway. So no, it can't loop indefinitely
The above should not encourage you to use such a design.
In line:
Out of line:Code:class foo { void bar() { // code here } };
There are further rules concerning where you place what part of the code - generally, the class definition goes into a header (.h or .hpp) and the out-of-line function definitions go into a source file (.cpp).Code:class foo { void bar(); }; void foo::bar() { // code here }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law