Polymorphisme se passe pas? c + +

  • Bozebo
  • Expert
  • Expert
  • Avatar de l’utilisateur
  • Inscription: Fév 15, 2006
  • Messages: 709
  • Loc: 404
  • Status: Offline

Message Janvier 19th, 2010, 7:25 am

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:

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;
}
 
 
  1. #include <iostream>
  2. #include <vector>
  3. #include <windows.h>
  4.  
  5. /*
  6.   Parent class to all objects which are part of the main game loop.
  7.   Examples:
  8.   static props
  9.   physics props
  10.   players
  11.   enemies
  12.   vehicles
  13.   explosion handlers
  14.   triggers
  15. */
  16. class CgameObject{
  17. public:
  18.   long x,y; //location
  19.   bool awake, visible;
  20.  
  21.   //constructor
  22.   CgameObject(long setX,long setY){
  23.     x = setX;
  24.     y = setY;
  25.     visible = true;
  26.     awake = true;
  27.   }
  28.  
  29.   virtual void step(){};
  30. };
  31.  
  32. #include <cmath>
  33. /*
  34.   Objects which have basic motion properties,
  35.   eg:
  36.   players
  37.   enemies
  38.   vehicles
  39.   physics props
  40. */
  41. class CmotionObject : public CgameObject{
  42. public:
  43.   double direction, //angle in degrees going anticlockwise from the +ve x axis
  44.   speed, //speed in pixels/frame along the direction (+ve or -ve)
  45.   vSpeed, //speed in pixels/frame along the y axis, down being +ve
  46.   hSpeed; //speed in pixels/frame along the x axis, right being +ve
  47.  
  48. public:
  49.   //constructor
  50.   CmotionObject(long setX,long setY):CgameObject(setX,setY){
  51.     vSpeed = 0;
  52.     hSpeed = 0;
  53.     speed = 0;
  54.     direction = 0;
  55.   }
  56.  
  57.   virtual void applyMotion(){
  58.     x += (long)hSpeed;
  59.     y += (long)vSpeed;
  60.   }
  61.  
  62.   /*
  63.     No getters required I think.
  64.   */
  65.   virtual void setVSpeed(double newSpeed){
  66.   }
  67.  
  68.   virtual void setHSpeed(double newSpeed){
  69.   }
  70.  
  71.   virtual void setSpeed(double newSpeed){
  72.   }
  73.  
  74.   virtual void setDirection(double newDirection){
  75.   }
  76. };
  77.  
  78. using namespace std;
  79. unsigned int i;
  80. unsigned long frameCount;
  81.  
  82. #include "classes/gameObject.h"
  83. #include "classes/motionObject.h"
  84. vector <CgameObject> gameObjects;
  85.  
  86. class Cplayer : public CmotionObject{
  87. public:
  88.   Cplayer(long setX,long setY):CmotionObject(setX,setY){};
  89.  
  90.   //do this every frame
  91.   void step(){
  92.     applyMotion();
  93.     cout << "player is at " << x << endl;
  94.     if(x < 100){
  95.       if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
  96.       hSpeed = 0; //stop moving
  97.     }
  98.   }
  99.  
  100. };
  101.  
  102. class Cnpc : public CmotionObject{
  103. protected:
  104.   int npcNumber;
  105.  
  106. public:
  107.   //constructor
  108.   Cnpc(long setX,long setY,int setNpcNumber):CmotionObject(setX,setY){
  109.     npcNumber = setNpcNumber;
  110.   }
  111.  
  112.   //do this every frame
  113.   void step(){
  114.     applyMotion();
  115.     cout << "npc #" << npcNumber << " is at " << x << endl;
  116.     if(x < 100){
  117.       if(x >= 100) cout << frameCount << ", npc has reached the finishing line\n";
  118.       hSpeed = 0; //stop moving
  119.     }
  120.   }
  121.  
  122. };
  123.  
  124. int main(){
  125.   frameCount = 0;
  126.  
  127.   gameObjects.push_back(Cplayer(0,30));
  128.   gameObjects.push_back(Cnpc(1,0,10));
  129.   gameObjects.push_back(Cnpc(2,0,20));
  130.   gameObjects[0].hSpeed = 4;
  131.  
  132.   while(true){
  133.     for(i = 0;i < gameObjects.size();i ++){
  134.       gameObjects[i].step();
  135.     }
  136.     frameCount ++;
  137.     cout << "frame #" << frameCount << " completed\n";
  138.     Sleep(100); //10 fps
  139.   }
  140.   return 0;
  141. }
  142.  
  143.  


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);
};
 
  1. class PS2Sprite
  2. {
  3. public:
  4.    PS2Sprite();
  5.    ~PS2Sprite();
  6.    virtual void Render(void);
  7. };
  8.  
  9. class PS2SpriteT : public PS2Sprite
  10. {
  11. public:
  12.    PS2SpriteT();
  13.    ~PS2SpriteT();
  14.    void Render(void);
  15. };
  16.  
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Janvier 19th, 2010, 7:25 am

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
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC