Polymorphisme se passe pas? c + +
- Bozebo
- Expert


- Inscription: Fév 15, 2006
- Messages: 709
- Loc: 404
- Status: Offline
Essayant d'obtenir un blocage d'exploiter pleinement la POO et je viens d'obtenir en permanence confus. Tout ce que je apprendre à lire un article ou un tutoriel va dans l'évier quand il ne fonctionne tout simplement pas.
Voici le code que j'utilise:
L'erreur, je veux en venir, le moment est la suivante:
C2039 erreur: "setHSpeed": n'est pas un membre de "CgameObject"
Je comprends que c'est la ligne:
gameObjects [0]. hSpeed = 4;
Et il est logique que gameObjects n'ont pas hSpeed tant que membre, mais sûre: motionObjects faire! N'est-il pas déclaré que:
"Le type du paramètre effectif peut être une classe dérivée de la classe du paramètre formel"
Par conséquent, je pense que ce serait sûrement travail? Sans définir la hSpeed Je ne reçois aucune erreur lors de l'exécution, mais il est en cours d'exécution que l'étape événements de la classe de base, qui ne fait rien.
edit:
Ce code est dans mes notes UNI...voir ce que je veux dire? Mine devrait fonctionner - ou ne vecteurs provoquer quelque chose d'étrange se produise.
Voici le code que j'utilise:
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;
- }
L'erreur, je veux en venir, le moment est la suivante:
C2039 erreur: "setHSpeed": n'est pas un membre de "CgameObject"
Je comprends que c'est la ligne:
gameObjects [0]. hSpeed = 4;
Et il est logique que gameObjects n'ont pas hSpeed tant que membre, mais sûre: motionObjects faire! N'est-il pas déclaré que:
"Le type du paramètre effectif peut être une classe dérivée de la classe du paramètre formel"
Par conséquent, je pense que ce serait sûrement travail? Sans définir la hSpeed Je ne reçois aucune erreur lors de l'exécution, mais il est en cours d'exécution que l'étape événements de la classe de base, qui ne fait rien.
edit:
Ce code est dans mes notes UNI...voir ce que je veux dire? Mine devrait fonctionner - ou ne vecteurs provoquer quelque chose d'étrange se produise.
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


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Janvier 19th, 2010, 7:25 am
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 1 message
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 119 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
