# Problem with a vector pointer

This is a discussion on Problem with a vector pointer within the C++ Programming forums, part of the General Programming Boards category; To put it brief: Code: class thisclass { public: vector <someclass> *sc; thisclass(vector <someclass> &_sc) { sc = &_sc; } ...

1. ## Problem with a vector pointer

To put it brief:

Code:
```class thisclass
{
public:
vector <someclass> *sc;
thisclass(vector <someclass> &_sc)
{
sc = &_sc;
}
void somefunc(const int n)
{
int x, int y;
if((x == someclass[n]->x) && (y == someclass[n]->y))
dosomething();
}
};```
Get what I'm trying to do here and why it doesn't work (that's the part I do not know)?

2. Code:
```void somefunc(const int n)
{
int x, int y;
if((x == someclass[n]->x) && (y == someclass[n]->y))
dosomething();
}```
You are comparing the value of x & y with vector elements without setting them to any real value.

Your compiler might issue a waring, but its something you should really look out for

3. Ah, that's just something I forget to do in the example, but consider as a real comparement in this situation. It should read something like:

int x = 13, y = 2;

But what I'm after is how to compare that 'x' element in the someclass-vector.

4. Ok....how doesnt it work?

What's the makeup of someclass??

5. Code:
```class Bomb
{
public:
Bomb(Player &_host, Map &_map, vector <Bomb> &_bombs)
{
...
bombs = &_bombs;
}
...
vector <Bomb> *bombs;
void do_flames(const short coord, const short t, const bool xy)
{
short _x = x, _y = y;
bool stop;
tile mapt;

for(int dir = 0;  dir < 2;  dir++)
for(int i = coord;;)
{
...
else if(isbomb(mapt))
for(size_t n = 0;  n < bombs->size();  n++)
if((_x == bombs[n]->x) && (_y == bombs[n]->y))
{
bombs[n]->timer = 0;
break;
}
}
}
...
};```
Results to:

[code]
G:\source\bombo>gxx main.cpp -lalleg -Wall
main.cpp: In member function `void Bomb::do_flames(short int, short int,
bool)':
main.cpp:254 (bombs[n]->x): base operand of `->' has non-pointer type `std::vector<Bomb,
std::allocator<Bomb> >'
main.cpp:254 (bombs[n]->y): base operand of `->' has non-pointer type `std::vector<Bomb,
std::allocator<Bomb> >'
main.cpp:256 (bombs[n]->timer): base operand of `->' has non-pointer type `std::vector<Bomb,
std::allocator<Bomb> >'
[/code}

6. vector <someclass> *sc; //??????????????

Code:
```this is a vector of type someclass called sc

vector <someclass> sc;

this is a vector of pointer to type someclass called sc

vector <someclass *> sc;

This is an iterator to be used with a vector of type someclass

vector <someclass>::iterator itr;

This is how you read through the vector using the iterator:

for(itr = sc.begin(); itr != sc.end(); ++)
{
//x, y, and timer are public member variables in class someclass
cout << itr->x << ' ' << itr->y << ' ' << itr->timer << endl;
}

this is how you read through the vector using [] operator
int index;

for(index = 0; index < sc.size(); ++index)
{
cout << sc[index].x << ' ' << sc[index].y << ' ' << sc[index].timer << endl;
}

this is how you would use [] if sc holds pointers to someclass objects

for(index = 0; index < sc.size(); ++index)
{
cout << sc[index]->x << ' ' << sc[index]->y << ' ' << sc[index]->timer << endl;
}

if you want to use an iterator to a vector of pointers to someclass it would look something like this:

vector <someclass *>::iterator pItr;
for(pItr = sc.begin(); pItr != sc.end(); ++pItr)
{
cout << (*pItr)->x << ' ' << (*pItr)->y << ' ' << (*pItr)->timer << endl;
}```
Assuming I haven't screwed up along the way.

7. So now I need to do a loop to the constructor like this (sorry, I don't have the time to test it first):
Code:
```Bomb(vector <Bomb> &_bombs)
{
for(size_t i = 0;  i < _bombs.size();  i++)  // I like this rather than those iterators...
boms[i] = &_bombs[i];
}
vector <Bomb *> bombs;```
But what does my first try, vector <Bomb> *bombs, do? Does it take 4 or bombs.size()*4 bytes of memory? Where does it point if I set it to &_bombs?