Jump to content
  • Advertisement
Sign in to follow this  
jdub

What's wrong with my C++ code?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

The function Game::Init is where it's spitting the error "Expecting a declaration" on a few lines. I included the header too.

Game.cpp:


#include "Game.h"
#include "allegro5\allegro.h"
#include <stdio.h>
Game::Game()
{

}
Game::~Game()
{
delete this->ball;
delete this->paddle1;
delete this->paddle2;
}
int Game::Init()
{

//init allegro
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
//create the display
this->display = al_create_display(640, 480);
if(!g_display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}

//init events
this->eventQueue = al_create_event_queue();
al_register_event_source(g_eventQueue, al_get_display_event_source(g_display));
if(!al_create_timer(this->GetTargetFPS()))
{
return -1;
}

this->ball = new Ball();
this->paddle1 = new Paddle();
this-> paddle2 = new Paddle();
return 0;
}


Game.h:


#include "allegro5\allegro.h"
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
class Paddle
{
public:
Paddle(int posX, int posY, int width, int height);
~Paddle();
int GetPosX() {return this->posX;}
int GetPosY() {return this->posY;}
int GetWidth() {return this->width;}
int GetHeight() {return this->height;}
void Draw();
void Update(long millis);

private:
ALLEGRO_BITMAP* paddleImg;
int posX, posY;
int width, height;
};
class Ball
{
public:
Ball(int posX, int posY);
~Ball();
int GetRadius();
int GetPosX();
int GetPosY();
int GetVelocityX();
int GetVelocityY();
void SetVelocity(int x, int y);
void Draw();
void Update(long millis);
private:
int radius;
int posX,posY;
};
class Game
{
private:
const double targetFPS = (int)(60.0f / 1000.0f);

Paddle* paddle1;
Paddle* paddle2;
Ball* ball;
ALLEGRO_EVENT_QUEUE* eventQueue;
ALLEGRO_DISPLAY* display;

public:
Game();
~Game();
double GetTargetFPS();
void Update(double millis);
void Draw();
int Init();
bool OnEvent(ALLEGRO_EVENT* evt);
}
#endif

Share this post


Link to post
Share on other sites
Advertisement
Missing semicolon after Game class declaration.

PS. There are few wrong things, but the one above is what causes compile error (for example deleting a bunch of pointers without checking if they are valid, not initializing them to NULL in constructor, so having a mess in those pointers till the moment where you call Init() method explicitly) Edited by noizex

Share this post


Link to post
Share on other sites
A note about noizex's recommendations:

Checking whether pointers are NULL before calling delete on them is unnecessary. Calling delete on a NULL pointer has no effect by definition. Although not initializing pointers to NULL in constructor(s) in current code is a problem. Edited by mrjones

Share this post


Link to post
Share on other sites

A note about noizex's recommendations:

Checking whether pointers are NULL before calling delete on them is unnecessary. Calling delete on a NULL pointer has no effect by definition. Although not initializing pointers to NULL in constructor(s) in current code is a problem.


Although initializing a pointer to NULL can make tracking down errors substantially simpler. For example, if I have a char* pointer that I assign the value "bob", then call delete on it, then print the value, it will *probably* print "bob", since that memory hasn't been reused yet. This is a live hand grenade of a bug waiting to go off, and hard as hell to find. If however, after deleting bob, you null it, trying to print it will cause your code to immediately explode, which is actually a very good thing.

It may not be required, but it can be good practice.



More to the point, the OP shouldn't be using naked pointers at all.

Just reread what I wrote and it sounds like I am disagreeing with mrjones. I am not, checking for null before delete is pointless. Edited by Serapth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!