Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


What's wrong with my C++ code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 jdub   Members   -  Reputation: 419

Like
0Likes
Like

Posted 11 July 2012 - 12:15 AM

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

J.W.

Sponsor:

#2 noizex   Members   -  Reputation: 882

Like
2Likes
Like

Posted 11 July 2012 - 12:32 AM

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, 11 July 2012 - 12:36 AM.


#3 mrjones   Members   -  Reputation: 612

Like
3Likes
Like

Posted 11 July 2012 - 01:58 AM

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, 11 July 2012 - 01:59 AM.


#4 Serapth   Crossbones+   -  Reputation: 5699

Like
0Likes
Like

Posted 11 July 2012 - 11:21 AM

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, 11 July 2012 - 11:23 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS