Reputation: 11
how do i access a variable inside a data structure inside a vector. I am using SDL 1.2
I am making a vector list which contains a data structure inside it called SDL_Rect
SDL_Rect has 4 veriables inside it as shown below:
typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;
i want to print out the "x" variable to the command prompt window. I am not sure what i am doing wrong?
vector< vector<SDL_Rect> > hitBoxArray;
I have a vector inside a vector with the data structure inside it. i am not sure if this makes a difference or not when writing my code below.
void (randomFunction)
{
int width = 10;
for (int i = 0; i < width; i++)
{
hitBoxArray.push_back(vector<SDL_Rect>());
cout<<hitBoxArray[1].x <---- this is the bit i cannot get to work
}
}
if you are wondering why i have a vector inside a vector, this is because this code is being used to create a tile map and the first vector is for the rows and the second vector is for the columns and that bit of code works fine. its only when i am trying to access the individual variables inside the data structure that is causing me issues.
below is my working code and also includes the non working code inside comments if that helps.
Function code inside class: tileDriver.cpp
void tile::readMapFile(string filename)
{
fstream map;
map.open(filename);
if(map.is_open())
{
map>>width;
map>>height;
cout<<"The width of the tile map is "<<width<<endl;
cout<<"The height of the tile map is "<<height<<endl<<endl;
for (int i = 0; i < width; i++)
{
tiles.push_back(vector<int>()); // Add an empty row for tile sprites
hitBoxArray.push_back(vector<SDL_Rect>()); // Add an empty row for hit boxes
//this bit is the code that will not work for me
cout<<hitBoxArray[1].x;
//this bit is the code that will not work for me
}
for (int j = 0; j < height; j++)
{
for (int i = 0; i < tiles.size(); i++)
{
tiles[i].push_back(i * j); // Add column to all rows for tile
sprites
map>>tiles[i][j];
}
for (int i = 0; i < hitBoxArray.size(); i++)
{
hitBoxArray[i].push_back(hitBox); // Add column to all rows for tile hit boxes
}
}
}
else
{
cout<<"unable to load map for tiled textures! did you spell the name correctly?";
}
}
header file: tileHeader.h
#include<iostream>
#include<fstream>
#include<vector>
#include "SDL.h"
using namespace std;
class tile
{
protected:
SDL_Surface* ground;
SDL_Surface* grass;
SDL_Rect groundTile;
SDL_Rect grassTile;
SDL_Rect hitBox;
SDL_Rect* temp;
vector< vector<SDL_Rect> > hitBoxArray;
int width;
int height;
vector< vector<int> > tiles;
public:
tile();
void readMapFile(std::string filename);
void printMap();
void show(SDL_Surface* scr);
void show(SDL_Surface* scr, SDL_Rect cam);
SDL_Rect getHitBox();
vector< vector<SDL_Rect> > getHitBoxArray();
};
this is the error i get inside my compiler:
Error 1 error C2039: 'x' : is not a member of 'std::vector<_Ty>' C:\Users\vishal\Desktop\game\game\tileDriver.cpp 52
i am using visual studio 2010 as my compliler
any help, incite or references to tutorials to help me solve my issue would be much appreciated :)
Upvotes: 1
Views: 252