c + +, avec l'aide de vecteurs

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

Message Août 1st, 2009, 6:25 am

Salut, Je cherche à utiliser des vecteurs pour la première fois et j'ai donc fait un exemple d'usage de les comprendre.
Code: [ Select ]
 
#include <iostream>
#include <vector>
 
using namespace std;
 
struct data{
  int location,hp;
  data(int x,int y, int hits){
    location = x<<8+y;
    hp = hits;
  }
 
  void showData(){
    cout << endl <<
    "location: " << getX() << "," << getY() << endl <<
    "hp: " << hp << endl << endl;
  }
 
  int getX(){
    return location >> 8;
  }
 
  int getY(){
    return location << 24 >> 24;
  }
};
 
vector <data> Vdata;
 
Vdata.push_back(4,7,2);
Vdata.push_back(9,3,1);
Vdata.push_back(2,11,7);
 
int main(){
  for(int i = 0;i < Vdata.size();i ++){
    Vdata[i].showData();
  }
  return 0;
}
 
 
  1.  
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct data{
  8.   int location,hp;
  9.   data(int x,int y, int hits){
  10.     location = x<<8+y;
  11.     hp = hits;
  12.   }
  13.  
  14.   void showData(){
  15.     cout << endl <<
  16.     "location: " << getX() << "," << getY() << endl <<
  17.     "hp: " << hp << endl << endl;
  18.   }
  19.  
  20.   int getX(){
  21.     return location >> 8;
  22.   }
  23.  
  24.   int getY(){
  25.     return location << 24 >> 24;
  26.   }
  27. };
  28.  
  29. vector <data> Vdata;
  30.  
  31. Vdata.push_back(4,7,2);
  32. Vdata.push_back(9,3,1);
  33. Vdata.push_back(2,11,7);
  34.  
  35. int main(){
  36.   for(int i = 0;i < Vdata.size();i ++){
  37.     Vdata[i].showData();
  38.   }
  39.   return 0;
  40. }
  41.  
  42.  


Toutefois, le compilateur gcc semble penser que les lignes
Code: [ Select ]
 
Vdata.push_back(4,7,2);
Vdata.push_back(9,3,1);
Vdata.push_back(2,11,7);
 
  1.  
  2. Vdata.push_back(4,7,2);
  3. Vdata.push_back(9,3,1);
  4. Vdata.push_back(2,11,7);
  5.  

sont problématiques. Appare notly la question est la suivante: "attendu constructeur, destructeur, ou la conversion de type avant. Token expected` ou `; auparavant. Token"
Mais tous les tutoriaux, j'ai vu le faire la même manière que je suis, en outre, devcpp montre l'astuce pour la méthode push_back. J'ai essayé d'utiliser "->" mais cela ne l'aide pas. Le compilateur semble ne pas être entendu que "Vdata" est un objet.
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Août 1st, 2009, 6:25 am

  • mk27
  • Proficient
  • Proficient
  • Avatar de l’utilisateur
  • Inscription: Juin 09, 2009
  • Messages: 334
  • Status: Offline

Message Août 1st, 2009, 7:40 am

I love C, mais ne connaissent pas de C + +. Essayez:

http://cboard.cprogramming.com/

Theres un forum C + +, vous aurez plus de conseils que vous savez quoi faire avec.
Image
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • Avatar de l’utilisateur
  • Inscription: Juil 25, 2005
  • Messages: 2735
  • Loc: Nashville, TN
  • Status: Offline

Message Août 1st, 2009, 7:45 am

Quote:
http://cboard.cprogramming.com/

Theres un forum C + +, vous aurez plus de conseils que vous savez quoi faire avec.


Boo, sifflement, boo. Weve eu quelques têtes C + + ici. :P
I'd love to change the world, but they won't give me the source code.
  • Bozebo
  • Expert
  • Expert
  • Avatar de l’utilisateur
  • Inscription: Fév 15, 2006
  • Messages: 709
  • Loc: 404
  • Status: Offline

Message Août 1st, 2009, 9:44 am

Ive a laissé tomber l'utilisation de ce vecteur. (sera l'aide de vecteurs dans l'environnement d'exécution principal, toutefois). Maintenant, je me fais des erreurs vraiment bizarre.
Code:
Code: [ Select ]
 
#ifndef levelsFile
#define levelsFile
 
//struct used to hold the block locations and healths for each level
struct SblockData{
  u16 location; //8 bit x and y locations in a 16 bit value
  u8 hp; //hits to destroy
 
  //constructor
  SblockData(u8 x,u8 y,u8 setHp){
    location = blockPos(x,y);
    hp = setHp;
  }
 
  //returns a 16 bit value for the block's co-ordinates
  u16 blockPos(u8 x,u8 y){
    //put x in the left half and y in the right half
    return (x<<8)|y;
  }
};
 
//struct used to hold each level
struct SlevelData{
  //level id
  u8 id;
  //dynamic array of blocks
  SblockData* blocks;
  //number of blocks
  u8 blockCount;
 
  //set level data
  void setId(u8 setId){
    id = setId;
    blockCount = 0;
  }
 
  //adds a block to the level
  void addBlock(u8 x,u8 y,u8 hp){
    blocks[blockCount] = SblockData(x,y,hp);
    blockCount ++;
  }
};
 
const u8 numLevels = 3;
 
const SlevelData* levelData;
 
for(int i=0;i < numLevels;i++)
  SlevelData[i] = new SlevelData;
 
levelData[0].setId(0);
levelData[0].addBlock(32,32,1);
levelData[0].addBlock(48,32,1);
levelData[0].addBlock(64,32,1);
levelData[0].addBlock(80,32,1);
levelData[0].addBlock(96,32,1);
 
levelData[1].setId(1);
levelData[1].addBlock(112,64,2);
levelData[1].addBlock(112,72,2);
levelData[1].addBlock(112,80,2);
levelData[1].addBlock(112,88,2);
 
levelData[2].setId(2);
levelData[2].addBlock(64,64,4);
levelData[2].addBlock(80,72,3);
levelData[2].addBlock(96,80,2);
levelData[2].addBlock(112,88,1);
levelData[2].addBlock(128,88,1);
levelData[2].addBlock(144,80,2);
levelData[2].addBlock(160,72,3);
levelData[2].addBlock(176,64,4);
 
#endif
 
 
  1.  
  2. #ifndef levelsFile
  3. #define levelsFile
  4.  
  5. //struct used to hold the block locations and healths for each level
  6. struct SblockData{
  7.   u16 location; //8 bit x and y locations in a 16 bit value
  8.   u8 hp; //hits to destroy
  9.  
  10.   //constructor
  11.   SblockData(u8 x,u8 y,u8 setHp){
  12.     location = blockPos(x,y);
  13.     hp = setHp;
  14.   }
  15.  
  16.   //returns a 16 bit value for the block's co-ordinates
  17.   u16 blockPos(u8 x,u8 y){
  18.     //put x in the left half and y in the right half
  19.     return (x<<8)|y;
  20.   }
  21. };
  22.  
  23. //struct used to hold each level
  24. struct SlevelData{
  25.   //level id
  26.   u8 id;
  27.   //dynamic array of blocks
  28.   SblockData* blocks;
  29.   //number of blocks
  30.   u8 blockCount;
  31.  
  32.   //set level data
  33.   void setId(u8 setId){
  34.     id = setId;
  35.     blockCount = 0;
  36.   }
  37.  
  38.   //adds a block to the level
  39.   void addBlock(u8 x,u8 y,u8 hp){
  40.     blocks[blockCount] = SblockData(x,y,hp);
  41.     blockCount ++;
  42.   }
  43. };
  44.  
  45. const u8 numLevels = 3;
  46.  
  47. const SlevelData* levelData;
  48.  
  49. for(int i=0;i < numLevels;i++)
  50.   SlevelData[i] = new SlevelData;
  51.  
  52. levelData[0].setId(0);
  53. levelData[0].addBlock(32,32,1);
  54. levelData[0].addBlock(48,32,1);
  55. levelData[0].addBlock(64,32,1);
  56. levelData[0].addBlock(80,32,1);
  57. levelData[0].addBlock(96,32,1);
  58.  
  59. levelData[1].setId(1);
  60. levelData[1].addBlock(112,64,2);
  61. levelData[1].addBlock(112,72,2);
  62. levelData[1].addBlock(112,80,2);
  63. levelData[1].addBlock(112,88,2);
  64.  
  65. levelData[2].setId(2);
  66. levelData[2].addBlock(64,64,4);
  67. levelData[2].addBlock(80,72,3);
  68. levelData[2].addBlock(96,80,2);
  69. levelData[2].addBlock(112,88,1);
  70. levelData[2].addBlock(128,88,1);
  71. levelData[2].addBlock(144,80,2);
  72. levelData[2].addBlock(160,72,3);
  73. levelData[2].addBlock(176,64,4);
  74.  
  75. #endif
  76.  
  77.  

et les erreurs sur la ligne 48 seule:
Quote:
levels.h: 48: erreur: expected non qualifiés-id avant "pour"
levels.h: 48: erreur: constructeur prévu, destructeur, ou la conversion de type avant <jeton
levels.h: 48: erreur: constructeur prévu, destructeur, ou la conversion de type avant + + jeton

Afficher de l'information

  • Total des messages de ce sujet: 4 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 171 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