Polymorphism not happening? c++
- Bozebo
- Expert


- Joined: Feb 15, 2006
- Posts: 709
- Loc: 404
- Status: Offline
Trying to get a hang of fully using OOP and I just get continually confused. Everything I learn reading an article or tutorial goes down the drain when it just doesn't work.
Here is the code I am using:
The error I am getting at the moment is:
error C2039: 'setHSpeed' : is not a member of 'CgameObject'
I understand that is the line:
gameObjects[0].hSpeed = 4;
And it makes sense that gameObjects do not have hSpeed as a member, but sure: motionObjects do! Is it not stated that:
"the type of the actual parameter can be a class derived from the class of the formal parameter"
Therefore I would surely think it would work? Without setting the hSpeed I get no errors but during runtime it is only running the step events of the base class, which does nothing.
edit:
This code is in my uni notes... see what I mean? Mine should work - or do vectors cause something strange to happen.
Here is the code I am using:
CPP Code: [ Select ]
#include <iostream>
#include <vector>
#include <windows.h>
/*
Parent class to all objects which are part of the main game loop.
Examples:
static props
physics props
players
enemies
vehicles
explosion handlers
triggers
*/
class CgameObject{
public:
long x,y; //location
bool awake, visible;
//constructor
CgameObject(long setX,long setY){
x = setX;
y = setY;
visible = true;
awake = true;
}
virtual void step(){};
};
#include <cmath>
/*
Objects which have basic motion properties,
eg:
players
enemies
vehicles
physics props
*/
class CmotionObject : public CgameObject{
public:
double direction, //angle in degrees going anticlockwise from the +ve x axis
speed, //speed in pixels/frame along the direction (+ve or -ve)
vSpeed, //speed in pixels/frame along the y axis, down being +ve
hSpeed; //speed in pixels/frame along the x axis, right being +ve
public:
//constructor
CmotionObject(long setX,long setY):CgameObject(setX,setY){
vSpeed = 0;
hSpeed = 0;
speed = 0;
direction = 0;
}
virtual void applyMotion(){
x += (long)hSpeed;
y += (long)vSpeed;
}
/*
No getters required I think.
*/
virtual void setVSpeed(double newSpeed){
}
virtual void setHSpeed(double newSpeed){
}
virtual void setSpeed(double newSpeed){
}
virtual void setDirection(double newDirection){
}
};
using namespace std;
unsigned int i;
unsigned long frameCount;
#include "classes/gameObject.h"
#include "classes/motionObject.h"
vector <CgameObject> gameObjects;
class Cplayer : public CmotionObject{
public:
Cplayer(long setX,long setY):CmotionObject(setX,setY){};
//do this every frame
void step(){
applyMotion();
cout << "player is at " << x << endl;
if(x < 100){
if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
hSpeed = 0; //stop moving
}
}
};
class Cnpc : public CmotionObject{
protected:
int npcNumber;
public:
//constructor
Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
npcNumber = setNpcNumber;
}
//do this every frame
void step(){
applyMotion();
cout << "npc #" << npcNumber << " is at " << x << endl;
if(x < 100){
if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
hSpeed = 0; //stop moving
}
}
};
int main(){
frameCount = 0;
gameObjects.push_back(Cplayer(0,30));
gameObjects.push_back(Cnpc(1,0,10));
gameObjects.push_back(Cnpc(2,0,20));
gameObjects[0].hSpeed = 4;
while(true){
for(i = 0;i < gameObjects.size();i ++){
gameObjects[i].step();
}
frameCount ++;
cout << "frame #" << frameCount << " completed\n";
Sleep(100); //10 fps
}
return 0;
}
#include <vector>
#include <windows.h>
/*
Parent class to all objects which are part of the main game loop.
Examples:
static props
physics props
players
enemies
vehicles
explosion handlers
triggers
*/
class CgameObject{
public:
long x,y; //location
bool awake, visible;
//constructor
CgameObject(long setX,long setY){
x = setX;
y = setY;
visible = true;
awake = true;
}
virtual void step(){};
};
#include <cmath>
/*
Objects which have basic motion properties,
eg:
players
enemies
vehicles
physics props
*/
class CmotionObject : public CgameObject{
public:
double direction, //angle in degrees going anticlockwise from the +ve x axis
speed, //speed in pixels/frame along the direction (+ve or -ve)
vSpeed, //speed in pixels/frame along the y axis, down being +ve
hSpeed; //speed in pixels/frame along the x axis, right being +ve
public:
//constructor
CmotionObject(long setX,long setY):CgameObject(setX,setY){
vSpeed = 0;
hSpeed = 0;
speed = 0;
direction = 0;
}
virtual void applyMotion(){
x += (long)hSpeed;
y += (long)vSpeed;
}
/*
No getters required I think.
*/
virtual void setVSpeed(double newSpeed){
}
virtual void setHSpeed(double newSpeed){
}
virtual void setSpeed(double newSpeed){
}
virtual void setDirection(double newDirection){
}
};
using namespace std;
unsigned int i;
unsigned long frameCount;
#include "classes/gameObject.h"
#include "classes/motionObject.h"
vector <CgameObject> gameObjects;
class Cplayer : public CmotionObject{
public:
Cplayer(long setX,long setY):CmotionObject(setX,setY){};
//do this every frame
void step(){
applyMotion();
cout << "player is at " << x << endl;
if(x < 100){
if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
hSpeed = 0; //stop moving
}
}
};
class Cnpc : public CmotionObject{
protected:
int npcNumber;
public:
//constructor
Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
npcNumber = setNpcNumber;
}
//do this every frame
void step(){
applyMotion();
cout << "npc #" << npcNumber << " is at " << x << endl;
if(x < 100){
if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
hSpeed = 0; //stop moving
}
}
};
int main(){
frameCount = 0;
gameObjects.push_back(Cplayer(0,30));
gameObjects.push_back(Cnpc(1,0,10));
gameObjects.push_back(Cnpc(2,0,20));
gameObjects[0].hSpeed = 4;
while(true){
for(i = 0;i < gameObjects.size();i ++){
gameObjects[i].step();
}
frameCount ++;
cout << "frame #" << frameCount << " completed\n";
Sleep(100); //10 fps
}
return 0;
}
- #include <iostream>
- #include <vector>
- #include <windows.h>
- /*
- Parent class to all objects which are part of the main game loop.
- Examples:
- static props
- physics props
- players
- enemies
- vehicles
- explosion handlers
- triggers
- */
- class CgameObject{
- public:
- long x,y; //location
- bool awake, visible;
- //constructor
- CgameObject(long setX,long setY){
- x = setX;
- y = setY;
- visible = true;
- awake = true;
- }
- virtual void step(){};
- };
- #include <cmath>
- /*
- Objects which have basic motion properties,
- eg:
- players
- enemies
- vehicles
- physics props
- */
- class CmotionObject : public CgameObject{
- public:
- double direction, //angle in degrees going anticlockwise from the +ve x axis
- speed, //speed in pixels/frame along the direction (+ve or -ve)
- vSpeed, //speed in pixels/frame along the y axis, down being +ve
- hSpeed; //speed in pixels/frame along the x axis, right being +ve
- public:
- //constructor
- CmotionObject(long setX,long setY):CgameObject(setX,setY){
- vSpeed = 0;
- hSpeed = 0;
- speed = 0;
- direction = 0;
- }
- virtual void applyMotion(){
- x += (long)hSpeed;
- y += (long)vSpeed;
- }
- /*
- No getters required I think.
- */
- virtual void setVSpeed(double newSpeed){
- }
- virtual void setHSpeed(double newSpeed){
- }
- virtual void setSpeed(double newSpeed){
- }
- virtual void setDirection(double newDirection){
- }
- };
- using namespace std;
- unsigned int i;
- unsigned long frameCount;
- #include "classes/gameObject.h"
- #include "classes/motionObject.h"
- vector <CgameObject> gameObjects;
- class Cplayer : public CmotionObject{
- public:
- Cplayer(long setX,long setY):CmotionObject(setX,setY){};
- //do this every frame
- void step(){
- applyMotion();
- cout << "player is at " << x << endl;
- if(x < 100){
- if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
- hSpeed = 0; //stop moving
- }
- }
- };
- class Cnpc : public CmotionObject{
- protected:
- int npcNumber;
- public:
- //constructor
- Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
- npcNumber = setNpcNumber;
- }
- //do this every frame
- void step(){
- applyMotion();
- cout << "npc #" << npcNumber << " is at " << x << endl;
- if(x < 100){
- if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
- hSpeed = 0; //stop moving
- }
- }
- };
- int main(){
- frameCount = 0;
- gameObjects.push_back(Cplayer(0,30));
- gameObjects.push_back(Cnpc(1,0,10));
- gameObjects.push_back(Cnpc(2,0,20));
- gameObjects[0].hSpeed = 4;
- while(true){
- for(i = 0;i < gameObjects.size();i ++){
- gameObjects[i].step();
- }
- frameCount ++;
- cout << "frame #" << frameCount << " completed\n";
- Sleep(100); //10 fps
- }
- return 0;
- }
The error I am getting at the moment is:
error C2039: 'setHSpeed' : is not a member of 'CgameObject'
I understand that is the line:
gameObjects[0].hSpeed = 4;
And it makes sense that gameObjects do not have hSpeed as a member, but sure: motionObjects do! Is it not stated that:
"the type of the actual parameter can be a class derived from the class of the formal parameter"
Therefore I would surely think it would work? Without setting the hSpeed I get no errors but during runtime it is only running the step events of the base class, which does nothing.
edit:
This code is in my uni notes... see what I mean? Mine should work - or do vectors cause something strange to happen.
CPP Code: [ Select ]
class PS2Sprite
{
public:
PS2Sprite();
~PS2Sprite();
virtual void Render(void);
};
class PS2SpriteT : public PS2Sprite
{
public:
PS2SpriteT();
~PS2SpriteT();
void Render(void);
};
{
public:
PS2Sprite();
~PS2Sprite();
virtual void Render(void);
};
class PS2SpriteT : public PS2Sprite
{
public:
PS2SpriteT();
~PS2SpriteT();
void Render(void);
};
- class PS2Sprite
- {
- public:
- PS2Sprite();
- ~PS2Sprite();
- virtual void Render(void);
- };
- class PS2SpriteT : public PS2Sprite
- {
- public:
- PS2SpriteT();
- ~PS2SpriteT();
- void Render(void);
- };
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
January 19th, 2010, 7:25 am
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 1 post
- Users browsing this forum: No registered users and 200 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
