Archived

This topic is now archived and is closed to further replies.

CloudNine

Inheritance related problem

Recommended Posts

CloudNine    224
Yo, I've been working on a clone of Breakout, and have been converting it to OOP principles. I've been inheriting from a base class, which several objects inherit from and provide their own Update function. However, whenever I seem to inherit from the class, it always gives me a parse error. I reckon there is a syntax error with one of the functions in the base class, but when I syntax-check the file, it comes out ok. Here's the code: BBase.h
#ifndef BASE_H
#define BASE_H

class BBase //All game objects inherit from this class

{
 public:
 
 BBase();
 virtual ~BBase();
 
 void CreateObject(float x,float y,float width,float height);
 void SetObjectColor(unsigned char r,unsigned char g,unsigned char b);
 
 virtual void Update()=0;
 
 void Draw();
 
 inline float GetX() {return x;}
 inline float GetY() {return y;}
 
 inline float GetR() {return r;}
 inline float GetG() {return g;}
 inline float GetB() {return b;}
 
 protected:
 
 float x,y;
 float width,height;
 unsigned char r,g,b;
}; 

#endif
BBase.cpp
#include "base.h"

BBase::BBase()
{
x=y=width=height=0;
r=g=b=255;
}

BBase::~BBase()
{
}

void BBase::CreateObject(float x,float y,float width,float height)
{
 this->x=x;
 this->y=y;
 this->width=width;
 this->height=height;
}

void BBase::SetObjectColor(unsigned char r,unsigned char g,unsigned char b)
{
 this->r=r;
 this->g=g;
 this->b=b;
}  

void BBase::Draw()
{
  glPushMatrix();
  glTranslatef(x,y,-25);
  glColor3ub(r,g,b);
  glBegin(GL_QUADS);
   glVertex2f(width,0); //Top right

   glVertex2f(0,0); //Top left

   glVertex2f(0,height); //Bottom left

   glVertex2f(width,height); //Bottom right

  glEnd();
 glPopMatrix();
} 
And a sample object, BBall
BBall.h
   
#ifndef BALL_H
#define BALL_H

class BBALL : public BBase
{ //<- Parse error here

 public:
 BBALL();
 ~BBALL();
 
 void Update();
 
 //Accessor functions

 inline float GetXDir() {return dx;}
 inline float GetYDir() {return dy;}
 
 inline void SetDelta(float dx,float dy) {this->dx=dx; this->dy=dy;}
 
 private:
 float dx,dy;
}; 

#endif
The headers are included like this in the main file:
#include "main.h"
#include "base.h"
#include "ball.h"
#include "bat.h"
#include "block.h"
#include "collision.h"

char cAppTitle[]="Breakout";
bool bFullscreen=true;
  
Any ideas as to what could be causing the problem? [edited by - CloudNine on August 6, 2003 9:01:05 AM]

Share this post


Link to post
Share on other sites
civguy    308
> I''ve been inheriting from a base class, which several objects inherit from
classes

> And a sample object, BBall
class

Sorry, I had to.

What is the parse error you''re getting? By the way, in ball.h beginning you should put

#ifndef BALL_H
#define BALL_H

#include "base.h"

class BBALL : public BBase {

It won''t solve the problem since you happen to already include base.h before ball.h in the main file, but it''s generally a good idea to make headers so that they can be included alone.

Share this post


Link to post
Share on other sites
Sneftel    1788
No, it couldn''t. What''s the error you''re getting?


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
stubble    158
Well - without seeing the error message you''re getting it''s hard - but the obvious thing from what you''ve posted is that BALL doesn''t implement the pure virtual update(..) method. This could be because you just haven''t posted the body file - or I''ve missed it in your post. If not, and you are trying to create an instance of the ball class, you will get a compiler error.

Share this post


Link to post
Share on other sites
CloudNine    224
Here''s the body file of BBall.cpp


#include "ball.h"

BBALL::BBALL()
{
x=y=dx=dy=width=height=0; //Set all variables to default value

r=g=b=255;
}

BBALL::~BBALL()
{
}

void BBALL::Update()
{
x+=dx;
y+=dy;

//Left and right check


if ((x>=6.5) || (x<=-6.5))
{
dx=-dx;
}

//Top check


if (y>=4.0f)
{
dy=-dy;
}
}


I think I do implement the Update() function correctly

Share this post


Link to post
Share on other sites
CloudNine    224
Here you go:


5 - bat.h - parse error before `{'' token
8 - bat.h - destructors must be member functions
10 - bat.h - parse error before `}'' token
9 - bat.cpp - invalid use of undefined type `class BBAT''
5 - bat.cpp - `y'' undeclared (first use this function) (y is inherited)


The same occurs for the rest of the object. I''ll try to upload the source today, and see if I can find a solution to this problem :/


Share this post


Link to post
Share on other sites