Page 1 of 2

[SOLVED] Help with Declaration Problem

Posted: Wed Jun 23, 2010 5:40 pm
by xiphirx
Hello,

I have been programming a class called mapEditor, and alongside of it, a very shitty GUI package. The mapEditor class includes the GUI like so

Code: Select all

#include "xi_window.h"

class mapEditor {
...
in the GUI, I need a pointer to a mapEditor class for a specific component, but the mapEditor class is declared AFTER the GUI, so it spits

Code: Select all

 error C2061: syntax error : identifier 'mapEditor'
The mapEditor needs to have the GUI declared first, and the GUI needs the mapEditor to be declared first. How do I resolve this? Is there something with inheritance I can do to declare a type that would suffice for a mapEditor pointer? :(

Please don't tell me I'm a jackass and need to rewrite D:

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 5:49 pm
by avansc
then use forward declaration.

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 6:06 pm
by xiphirx
avansc wrote:then use forward declaration.
So, before I include the GUI things in my mapEditor class header, I put

Code: Select all

class mapEditor;

?

I did this, and now it's complaining about an undefined type 'mapEditor'.

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 8:32 pm
by Ginto8
xiphirx wrote:
avansc wrote:then use forward declaration.
So, before I include the GUI things in my mapEditor class header, I put

Code: Select all

class mapEditor;

?

I did this, and now it's complaining about an undefined type 'mapEditor'.
every situation in which GUI or w/e is included, you also need to include the mapEditor definition.

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 8:40 pm
by Falco Girgis
You should ALWAYS be using class prototypes unless you MUST have the actual class declaration within the header file. If it's a pointer, reference, or passing of a parameter, you don't need to be including the header file.

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 8:43 pm
by Ginto8
GyroVorbis wrote:You should ALWAYS be using class prototypes unless you MUST have the actual class declaration within the header file. If it's a pointer, reference, or passing of a parameter, you don't need to be including the header file.
sorry I meant prototype not definition :lol:

Re: Help with Declaration Problem

Posted: Wed Jun 23, 2010 9:09 pm
by xiphirx
GyroVorbis wrote:You should ALWAYS be using class prototypes unless you MUST have the actual class declaration within the header file. If it's a pointer, reference, or passing of a parameter, you don't need to be including the header file.
I've never really dealt with class prototypes, so this is a bit confusing...

So say I had the class mapEditor, and another class called otherClass. In otherClass's header, I want to use the class mapEditor, so I would just prototype the class mapEditor?

Re: Help with Declaration Problem

Posted: Thu Jun 24, 2010 3:46 am
by MrDeathNote
xiphirx wrote:
GyroVorbis wrote:You should ALWAYS be using class prototypes unless you MUST have the actual class declaration within the header file. If it's a pointer, reference, or passing of a parameter, you don't need to be including the header file.
I've never really dealt with class prototypes, so this is a bit confusing...

So say I had the class mapEditor, and another class called otherClass. In otherClass's header, I want to use the class mapEditor, so I would just prototype the class mapEditor?
Yip thats pretty much the idea of it.

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 2:52 pm
by xiphirx
Am I missing something here? I successfully prototyped my mapEditor class, and it recognizes it, but whenever I use one of the pointed to class's methods, the compiler says it's undefined...

So, if you are using forward declaration, you can just store pointers? you cant actually use them? :/

EDIT:

I'm passing the pointer to the mapEditor class as an argument to a function,

Code: Select all

void button::onClick(map * theMap, mapEditor * editor)
later down in the code, this is what causes an error

Code: Select all

editor->newMap(openFileName());

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 3:52 pm
by RyanPridgeon
xiphirx wrote:Am I missing something here? I successfully prototyped my mapEditor class, and it recognizes it, but whenever I use one of the pointed to class's methods, the compiler says it's undefined...

So, if you are using forward declaration, you can just store pointers? you cant actually use them? :/
Not when they've only been forward-declared. But this doesn't matter. In the header where you're forward declaring, you don't NEED to use them. You can include the entire class definition in the source file where you're actually defining the functions, but just use forward declaration in the header to avoid the include loop.

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 4:03 pm
by xiphirx
I'm becoming lost...
so I put

Code: Select all

class mapEditor;
#include "mapeditor.h"
?

...

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 4:15 pm
by lotios611
I think it should be

Code: Select all

#include "mapeditor.h"
class mapEditor;

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 4:17 pm
by xiphirx
lotios611 wrote:I think it should be

Code: Select all

#include "mapeditor.h"
class mapEditor;
Both ways are not working :(

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 5:12 pm
by K-Bal
You use the forward declaration in your header and the actual include in your source file.

Re: Help with Declaration Problem

Posted: Mon Jun 28, 2010 7:12 pm
by RyanPridgeon
xiphirx wrote:
lotios611 wrote:I think it should be

Code: Select all

#include "mapeditor.h"
class mapEditor;
Both ways are not working :(
No. Look, you should have a header with the class prototype, where you define the object as being part of your class. So for this, you don't need to know what the object does. You don't need to know its functions, members or usage. So you simply forward declare, such as

foo.h

Code: Select all

#ifndef SOMETHING
#define SOMETHING

class Bar;

class Foo {
    Bar* var;

    Foo();
};

#endif
Then when you want to use the object in your source file, you are free to INCLUDE the entire object's prototype, so that you can use the functions and members.

foo.cpp

Code: Select all

#include "foo.h"
#include "bar.h"

Foo::Foo(){
    var = new Bar();
    var->DoSomething();
    var->someMember = RANDOM_THING;
}



SO a recap. IN MOST CASES: In the header, you FORWARD DECLARE. In the source file, you INCLUDE.
edit:

That's about as good as I can explain it. I found this example code for you to look through if you're still having problems.
Example code for forward declaration
:) The trick is to think about what the compiler knows. If you say class Something; the compiler knows you have a class called Something, so you can hold pointers to it but you can't use it's functions because the compiler doesn't know about them.