c++, vectors

Associate
Joined
10 Oct 2008
Posts
353
Location
Dundee
Hi, I can't for the life of me figure out what I am doing wrong when I try and get vectors working. Here is my test program:
Code:
#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(data(4,7,2)); //updated this line
Vdata.push_back(data(9,3,1)); //updated this line
Vdata.push_back(data(2,11,7)); //updated this line

int main(){
  for(int i = 0;i < Vdata.size();i ++){
    Vdata[i].showData();
  }
  return 0;
}

It all seems perfect to me, but no matter how I implement vectors it always complains on the following lines:
Code:
Vdata.push_back(4,7,2); //note: I have updated these 3 lines (see above), I still get the error involving the "."
Vdata.push_back(9,3,1);
Vdata.push_back(2,11,7);
The error I get from gcc is:
expected constructor, destructor, or type conversion before '.' token expected `,' or `;' before '.' token

It's almost like it's not even considering Vdata as an instance, what am I doing wrong? All the tutorials and example codes I can find do it the same way. I also tried a test with an integer vector instead of a struct, got the same problem.

Thanks in advance
 
Last edited:
Code:
Vdata.push_back(data(4,7,2));
Vdata.push_back(data(9,3,1));
Vdata.push_back(data(2,11,7));
That'll work. You need to tell it what data type you're trying to add otherwise it assumes it's an int.
 
Code:
Vdata.push_back(4,7,2);
Vdata.push_back(9,3,1);
Vdata.push_back(2,11,7);
The error I get from gcc is:

try

vdata.push_back( data(4,7,2) );

etc

I think you'll also have to provide a default constructor and copy constructor for your data structure if you want to use it in a vector.

edit: beaten :(
 
Last edited:
Code:
Vdata.push_back(data(4,7,2));
Vdata.push_back(data(9,3,1));
Vdata.push_back(data(2,11,7));
That'll work. You need to tell it what data type you're trying to add otherwise it assumes it's an int.

Doesn't work. I get the same error, infact that is how I have it in the program which I am trying to learn vectors for in the first place.

Though perhaps if I wasn't getting my error then that would have been an issue later in this test.

edit:
Screenshot

edit:
forget the whole vectors thing now. I am doing it a different way with arrays... but it is of course also refusing to work:
Code:
#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 ++;
  }
};

/*
Character set for reference
0 = empty/space
1-10 = numbers 1234567890
11-27 = letters a-z
*/

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

Apparently:
levels.h:55: error: expected unqualified-id before 'for'

what is going on? :S
 
Last edited:
As it's still not working, there's something strange with your compiler. I copied the code you gave in your original post, put it in to visual studio and compiled it with the changes I suggested without any problems.
 
As it's still not working, there's something strange with your compiler. I copied the code you gave in your original post, put it in to visual studio and compiled it with the changes I suggested without any problems.

Ouch, that seems to be the cause of most of my programming problems >_<

I have noticed that it works if those lines are within main(), but the whole point in this is that the level information is hard coded in a header file. Right now I am just working on the code that runs the game then I will find a way to make it accept some kind of storage format. What I am aiming for is using const to make it save the information in ROM, as I am compiling for GBA. Oddly the issue that was prevalent here also occurred when compiling for windows.
 
Umm, i'm not that familiar with C++, but surely you can't call methods on objects unless they are within a function (or main section)?

In both cases, you're trying to call methods on objects (e.g. push_back on your VData object) that have not been instantiated, this will only occurr at run time.
 
Back
Top Bottom