Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Forward declaration???

Credits go to CursedTyrant, Indeterminatus, Jonatan Hedborg, Maikol, and Neil Walker for helping out!
This thread is locked; no one can reply to it. rss feed Print
Forward declaration???
affeheimer
Member #7,799
September 2006

#include <allegro.h>

using namespace std;

class Planet;

#define screen_w 1024
#define screen_h 768

BITMAP *earth;
BITMAP *buffer;

void show(Planet* thing)
{
clear_to_color(buffer, makecol(0, 0, 255));
draw_sprite(buffer, earth, thing->x-32, thing->y-32);
acquire_screen();
draw_sprite(screen, buffer, 0, 0);
rest(5);
readkey();
};

class Planet
{
public:
int x, y;
Planet()
{
x=(screen_w/2)-(earth->w/2);
y=(screen_h/2)-(earth->h/2);
};
};

int main()
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, screen_w, screen_h, 0, 0);
earth=load_bitmap("earth.bmp", NULL);
buffer=create_bitmap(screen_w, screen_h);
clear_to_color(buffer, makecol(0, 0, 255));
Planet jord;
show(&jord);
destroy_bitmap(buffer);
destroy_bitmap(earth);
return 0;
}

END_OF_MAIN();

Compiler error: In function `void show(Planet*)':
17 invalid use of undefined type `struct Planet'
5 forward declaration of `struct Planet'
17 invalid use of undefined type `struct Planet'
5 forward declaration of `struct 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
avatar

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
Aristóteles

Indeterminatus
Member #737
November 2000
avatar

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:

1class Planet
2{
3public:
4 int x, y;
5 Planet()
6 {
7 x=(screen_w/2)-(earth->w/2);
8 y=(screen_h/2)-(earth->h/2);
9 };
10 
11 void show(/*Planet* thing*/)
12 {
13 clear_to_color(buffer, makecol(0, 0, 255));
14 draw_sprite(buffer, earth, this->x-32, this->y-32);
15 acquire_screen();
16 draw_sprite(screen, buffer, 0, 0);
17 rest(5);
18 readkey();
19 }
20 
21};

The added benefit is that the compiler won't complain, and you don't need any forward declarations.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Neil Walker
Member #210
April 2000
avatar

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.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

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

1#include <allegro.h>
2#include "Planet.h"
3 
4class Planet;
5 
6using namespace std;
7 
8#define screen_w 1024
9#define screen_h 768
10 
11BITMAP *earth;
12BITMAP *buffer;
13 
14 
15int main()
16{
17 allegro_init();
18 install_keyboard();
19 set_color_depth(16);
20 set_gfx_mode(GFX_AUTODETECT, screen_w, screen_h, 0, 0);
21 earth=load_bitmap("earth.bmp", NULL);
22 buffer=create_bitmap(screen_w, screen_h);
23 clear_to_color(buffer, makecol(0, 0, 255));
24 Planet jord;
25 jord.show();
26 destroy_bitmap(buffer);
27 destroy_bitmap(earth);
28 delete jord;
29 return 0;
30};
31 
32END_OF_MAIN();

And this is the class header:
Planet.h:

1#include "Main.cpp"
2 
3class Planet
4{
5 public:
6 int x, y;
7 Planet()
8 {
9 x=(screen_w/2)-(earth->w/2);
10 y=(screen_h/2)-(earth->h/2);
11 };
12 void show(int x, int y)
13 {
14 clear_to_color(buffer, makecol(0, 0, 255));
15 draw_sprite(buffer, earth, x-32, y-32);
16 acquire_screen();
17 draw_sprite(screen, buffer, 0, 0);
18 release_screen();
19 rest(5);
20 readkey();
21 };
22};

Now when I compile it gets stuck and never finish.. When I click cancel I get a compiler log like this:
In file included from C:\Documents and Settings\Søren\Skrivebord\Project/Main.cpp:2,
from C:\Documents and Settings\Søren\Skrivebord\Project/Planet.h:1,
from C:\Documents and Settings\Søren\Skrivebord\Project/Main.cpp:2,
from C:\Documents and Settings\Søren\Skrivebord\Project/Planet.h:1,

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
avatar

Why on earth are you including the Cpp file in the header?
That causes an "endless loop"...
There are other things wrong too, but that should be the big one.

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

1#include <allegro.h>
2#include "Planet.h"
3 
4class Planet;
5 
6using namespace std;
7 
8const int screen_w=1024;
9const int screen_h=768;
10 
11BITMAP *earth;
12BITMAP *buffer;
13 
14void show(Planet* thing)
15 {
16 clear_to_color(buffer, makecol(0, 0, 255));
17 draw_sprite(buffer, earth, thing->x-32, thing->y-32);
18 acquire_screen();
19 draw_sprite(screen, buffer, 0, 0);
20 release_screen();
21 rest(5);
22 readkey();
23 };
24
25int main()
26{
27 allegro_init();
28 install_keyboard();
29 set_color_depth(16);
30 set_gfx_mode(GFX_AUTODETECT, screen_w, screen_h, 0, 0);
31 earth=load_bitmap("earth.bmp", NULL);
32 buffer=create_bitmap(screen_w, screen_h);
33 clear_to_color(buffer, makecol(0, 0, 255));
34 Planet jord;
35 show(&jord);
36 destroy_bitmap(buffer);
37 destroy_bitmap(earth);
38 delete &jord;
39 return 0;
40};
41 
42END_OF_MAIN();

And the header file:
Planet.h

1extern const int screen_w;
2extern const int screen_h;
3extern BITMAP* earth;
4extern BITMAP* buffer;
5 
6class Planet
7{
8 public:
9 int x, y;
10 Planet()
11 {
12 x=(screen_w/2)-(earth->w/2);
13 y=(screen_h/2)-(earth->h/2);
14 };
15 void show(int x, int y)
16 {
17 clear_to_color(buffer, makecol(0, 0, 255));
18 draw_sprite(buffer, earth, x-32, y-32);
19 acquire_screen();
20 draw_sprite(screen, buffer, 0, 0);
21 release_screen();
22 rest(5);
23 readkey();
24 };
25};

CursedTyrant
Member #7,080
April 2006
avatar

Remove everything before int main (except #include "Planet.h") in the Main.cpp file.

Planet.h

1#include "allegro.h"
2using namespace std;
3 
4BITMAP *earth;
5BITMAP *buffer;
6 
7class Planet
8{
9 public:
10 int x, y;
11
12 void show(int x, int y);
13
14 Planet();
15};
16 
17Planet::Planet()
18{
19 x=(SCREEN_W/2)-(earth->w/2);
20 y=(SCREEN_H/2)-(earth->h/2);
21};
22void Planet::Show(int x, int y)
23{
24 clear_to_color(buffer, makecol(0, 0, 255));
25 draw_sprite(buffer, earth, x-32, y-32);
26 acquire_screen();
27 draw_sprite(screen, buffer, 0, 0);
28 release_screen();
29 rest(5);
30 readkey();
31}

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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

affeheimer
Member #7,799
September 2006

THANK YOU everyone for the help. I really appreciate it :D

Go to: