objects declared in header file

I guess I should've been posting here all along. Anyway: I'm working on a game using an external game programming library (allegro) but I want to use an object as a generic data holder structure to test for collisions. object being used is "area" and its being used in player class called "program9handler"
area.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef AR_H
#define AR_H
class area
{
	public:
	area();
	~area();
	
	void initialize();
	struct pt {
		int x;
		int y;
		int set;
	} tl,tr,bl,br;
	int x,y,width,height;
	private:
};
#endif 

area.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "area.h"
area::area() 
{}
void area::initialize()
{
	tl.set=0;
	tl.x=0;
	tl.y=0;
	tr.set=0;
	tr.x=0;
	tr.y=0;
	bl.set=0;
	bl.x=0;
	bl.y=0;
	br.set=0;
	br.x=0;
	br.y=0;
}
area::~area()
{}

program9handler.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef AR_H
#define AR_H
#include "area.h"
#endif

#ifndef P9_H
#define P9_H
class BITMAP;
class area;
class program9handler
{
	public:
	...
	area areas[8];
        ...
	
};
#endif 

program9handler.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "program9handler.h"
#include <cmath>
#include <stdio.h>
#include <cstdlib>
#include <exception>
#include <allegro.h>
...
void program9handler::sethitboundaries(BITMAP* sprite,int z)
{
	int inc = (height/30);
	int mid = ((int)(sqrt((float)(width*width+height*height))))/2;
		for(int i=0;i<width/2;i++)
		{
			tl = inc*i;
			if(areas[z].tl.set==0)
			{
				//getpixel(b,tl,tl)
				if(magicpink!=getpixel(sprite,tl,tl))
				{
					areas[z].tl.set=1;
					areas[z].tl.x=tl;
					areas[z].tl.y=tl;
				}
			}
                        ...
                 }
}

console:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
jdd:p9wbullets jdd$ g++ -c program9handler.cpp
program9handler.h:31: error: field ‘areas’ has incomplete type
program9handler.cpp: In member function ‘void program9handler::sethitboundaries(BITMAP*, int)’:
program9handler.cpp:209: error: ‘tl’ was not declared in this scope
program9handler.cpp:210: error: ‘areas’ was not declared in this scope
program9handler.cpp:221: error: ‘areas’ was not declared in this scope
program9handler.cpp:231: error: ‘areas’ was not declared in this scope
program9handler.cpp:241: error: ‘areas’ was not declared in this scope
program9handler.cpp: In member function ‘void program9handler::checkcollision(program9handler)’:
program9handler.cpp:264: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:265: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:267: error: ‘areas’ was not declared in this scope
program9handler.cpp:278: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:279: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:281: error: ‘areas’ was not declared in this scope
program9handler.cpp:292: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:293: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:295: error: ‘areas’ was not declared in this scope
program9handler.cpp:306: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:307: error: ‘class program9handler’ has no member named ‘areas’
program9handler.cpp:309: error: ‘areas’ was not declared in this scope


In retrospect I probably didn't need to do that I just thought it would be easier for collision detection across multiple objects since I was trying to add the ability to shoot. the method above, as you can guess, is reading a bitmap and finding the areas where the color changes from the background color to something else to create a collision detection area.

But I'm curious why that wouldn't work.
Last edited on
you are using #endif directive twice but #ifndef only once
that means areah.h is not being included at all.
remove first #endif directive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef AR_H
#define AR_H
#include "area.h"
#endif

#ifndef P9_H
#define P9_H
class BITMAP;
class area;
class program9handler
{
	public:
	...
	area areas[8];
        ...
	
};
#endif  


it says it's incomplete because class is forward declared bu no declaration is being seen since area.h header is not included!

edit:
one more thing may help you in the future:
include headers before #ifndef #define directives.
Last edited on
Topic archived. No new replies allowed.