[SOLVED] Help with Declaration Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

[SOLVED] Help with Declaration Problem

Post 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:
Last edited by xiphirx on Sun Jul 11, 2010 5:54 pm, edited 1 time in total.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Help with Declaration Problem

Post by avansc »

then use forward declaration.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help with Declaration Problem

Post 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'.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help with Declaration Problem

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help with Declaration Problem

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help with Declaration Problem

Post 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:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help with Declaration Problem

Post 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?
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Help with Declaration Problem

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help with Declaration Problem

Post 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());
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Help with Declaration Problem

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help with Declaration Problem

Post by xiphirx »

I'm becoming lost...
so I put

Code: Select all

class mapEditor;
#include "mapeditor.h"
?

...
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Help with Declaration Problem

Post by lotios611 »

I think it should be

Code: Select all

#include "mapeditor.h"
class mapEditor;
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help with Declaration Problem

Post by xiphirx »

lotios611 wrote:I think it should be

Code: Select all

#include "mapeditor.h"
class mapEditor;
Both ways are not working :(
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Help with Declaration Problem

Post by K-Bal »

You use the forward declaration in your header and the actual include in your source file.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Help with Declaration Problem

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply