c++, help with vectors

  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

Hi, I am trying to use vectors for the first time so I have made an example usage to figure them out.
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.  


However, gcc seems to think the lines
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.  

are problematic. Apparently the issue is as follows: "expected constructor, destructor, or type conversion before '.' token expected `,' or `;' before '.' token"
But all the tutorials I have seen do it the same way I am, furthermore; devcpp is showing the hint for the push_back method. I have tried using "->" but that didn't help. The compiler seems to not be understanding that "Vdata" is an object.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Posts: 334

Post 3+ Months Ago

I love C, but know no C++. Try:

http://cboard.cprogramming.com/

There's a C++ forum, you will get more advice that you know what to do with.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

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

There's a C++ forum, you will get more advice that you know what to do with.


Boo, hiss, boo. We've got some C++ heads around here. :P
  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

I've ditched the vector usage for this. (will be using vectors in the main runtime though). Now I am getting really odd errors.
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.  

and the errors on line 48 alone:
Quote:
levels.h:48: error: expected unqualified-id before 'for'
levels.h:48: error: expected constructor, destructor, or type conversion before '<' token
levels.h:48: error: expected constructor, destructor, or type conversion before '++' token

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 116 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.