Public Group

# Header file problem

This topic is 4917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone. I'm having a lot of trouble with a header file I made. It's basically just a bunch of classes with all their variables, functions, definitions etc. The problem is that if I #include it, it makes the program hang, and if I try to declare an object from it I get one of those "LevEditor.exe has encountered a problem and needs to close. We are sorry for the inconvenience." I really have no idea what the problem with the header file is. Here is the source for the header, if it helps at all:
#include <SDL/SDL.h>

const int SCREENWIDTH  = 1024;
const int SCREENHEIGHT = 780;
const int SCREENBPP    = 32;

class Bar
{
public:
SDL_Surface* surface;

SDL_Rect barRect;

int hitsToDestroy;

double barMiddleX;

bool isDestroyed;

};

{

public:
SDL_Surface* surface;

};

{
paddleRect.x = SCREENWIDTH / 2;
paddleRect.y = SCREENHEIGHT + 130;

}

class Ball
{
public:
SDL_Surface* surface;

SDL_Rect ballRect;

double xVel;
double yVel;
double ballMiddleX;

bool CheckCollision(Bar bar);
void collide(Bar bar);
Ball();
};

Ball::Ball()
{

ballRect.x = SCREENWIDTH / 2;
ballRect.y = SCREENHEIGHT / 2;
ballRect.w = surface->w;
ballRect.h = surface->h;

ballMiddleX = ballRect.x + (ballRect.w/2);

xVel = 2;
yVel = 2;
}

bool Ball::CheckCollision(Bar bar)
{
/*Check if ball's Y axis is farther up than bar's Y axis and if ball's sides lie within bar's sides and if bar is destroyed*/
if             ((ballRect.y < (bar.barRect.y + bar.barRect.h)) && (ballRect.x >= bar.barRect.x) &&
((ballRect.x + ballRect.w) <= (bar.barRect.x + bar.barRect.w))
&& (bar.isDestroyed == false) && (yVel < 0))
return true;
else return false;
}

{
/*Check if ball's Y axis is farther down than paddle's Y axis and if ball's sides lie within paddle's sides*/
return true;
else return false;
}

void Ball::collide(Bar bar)
{
/*Check which side of the bar the ball hit on and collide appropriately*/
if ((ballMiddleX <= bar.barMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
if ((ballMiddleX <= bar.barMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
if ((ballMiddleX >  bar.barMiddleX) && xVel > 0) {yVel = -yVel; xVel = 1.5 * xVel;}
if ((ballMiddleX >  bar.barMiddleX) && xVel < 0) {yVel = -yVel; xVel = .5 * xVel;}

}

{
if ((ballMiddleX <= paddle.paddleMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
if ((ballMiddleX <= paddle.paddleMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
if ((ballMiddleX >  paddle.paddleMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
if ((ballMiddleX >  paddle.paddleMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
}

class Level
{
public:
Bar layout[10][4];
SDL_Surface* background;
Ball ball;

};


Like I said, it's just a bunch of classes and definitions, but when I #include it it causes the program to hang. I have no idea what's causing the problem. If anyone can help out, I'd really appreciate it. Thanks,

##### Share on other sites
I'm not sure why you'd get those errors just by including the file, but one thing you might want to do is add inclusion guards.

#ifndef YOUR_FILE_H#define YOUR_FILE_H// your code goes here#endif

But doing this will probably only fix linker errors, not runtime errors.
When you include this file, are you also calling code from one of these classes? If so, you might want to debug your code and step through it to find which line is causing the errors.

##### Share on other sites
If I do include code (declare a Level object, for instance), I get one of those messages saying that the program must close. Even if I don't include code from it, though, the program still hangs and won't run correctly. It really doesn't make sense. There's no linker problems or anything, the program compiles fine. It's runtime errors that have something to do with the header file, and I have no idea what's causing it.

##### Share on other sites
Run it through a debugger and tell us what line it hangs at.

EDIT: It's a "shot in the dark", but maybe the problem is with SDL_Surface* surface? After you load the .bmp file assert() that the pointer is valid. Also make sure that file is in the right place.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015658
×