![]() |
|
Forward declaration??? |
affeheimer
Member #7,799
September 2006
|
#include <allegro.h> using namespace std; class Planet; #define screen_w 1024 BITMAP *earth; void show(Planet* thing) class Planet int main() END_OF_MAIN(); Compiler error: In function `void show(Planet*)': Can someone help me make this compile.. I have already forward declared it at the beginning of the file.. thanks |
Maikol
Member #4,997
September 2004
![]() |
This topic ought to be in programing questions, not here. As well use Mockup to enlight syntax. Don't know C++ but I think you're treating Planet as a struct instead of a class. Do not use the arrow '->', I think the dot '.' is used to reference object's routines or attributes. Anyway there are semicolons after function's closing braces and you do not release the screen.
El sabio no dice todo lo que piensa, pero siempre piensa todo lo que dice |
Indeterminatus
Member #737
November 2000
![]() |
For an access of a member of Planet, the compiler needs the full type definition, so a forward declaration won't do in your case (mind the thing->x). The "arrow" -> is just fine. Now that thing is passed as pointer, you have to dereference it first to get to its members. Dereference with *, access with . - you'd have (*thing)., which can be written shorter as thing-> Anyway, looks like show is in fact a method of Planet, so I suggest you also make that syntactically clear by putting it into the class declaration. Thus, you end up with:
The added benefit is that the compiler won't complain, and you don't need any forward declarations. _______________________________ |
Neil Walker
Member #210
April 2000
![]() |
There are many reasons why the code doesn't work, but forward declarations are there mainly to avoid multiple #include statements header files. Basically, move your class definition to it's own header file, or put the class at the top of the file. But you should really think abour re-organising your code to be more object oriented. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
affeheimer
Member #7,799
September 2006
|
okay tried moving the class definition to a header file, and changing a few other things... Main.cpp
And this is the class header:
Now when I compile it gets stuck and never finish.. When I click cancel I get a compiler log like this: And the list goes on... And how do I make the x and y integer in my class go over to my function??? By the way I am using Dev-C++ thx. Sorry for the large post |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
Why on earth are you including the Cpp file in the header?
|
affeheimer
Member #7,799
September 2006
|
It makes a loop?? of course.. Thanks. Think my mind froze for a minute.. Just made it work.. But I think that there are a lot of unnecessary code that could be changed to be more compact??? Any suggestions??? here my my new code: Main.cpp
And the header file:
|
CursedTyrant
Member #7,080
April 2006
![]() |
Remove everything before int main (except #include "Planet.h") in the Main.cpp file. Planet.h
That should be much more readable, and it does exactly the same, since the function is declared in the class. It should work fine if you make those changes, but I didn't look too closely at the code, so there may be other hidden errors. One more thing: const int screen_w=1024; const int screen_h=768; You are using Allegro... use SCREEN_W and SCREEN_H (all capital) to use what Allegro provides. --------- |
affeheimer
Member #7,799
September 2006
|
THANK YOU everyone for the help. I really appreciate it |
|