Sign in to follow this  
Phoresis

C++ Vectors

Recommended Posts

Phoresis    122
I want to declare a pointer to an object stored inside a vector, but it won't let me.
TableComponent *t;
for(vector<TableComponent>::iterator iter = items.begin(); iter != items.end(); iter++) 
{
  if (test == iter->getId())
  {
    t = iter;
  }
}
What am I doing wrong?

Share this post


Link to post
Share on other sites
Simian Man    1022
The types TableComponent* and vector<TableComponent>::iterator are not compatible. Just use the iterator type instead - it can do everything a pointer can.

vector<TableComponent>::iterator t;

for(vector<TableComponent>::iterator iter = items.begin(); iter != items.end(); iter++)
{
if (test == iter->getId())
{
t = iter;
}
}


HTH

Share this post


Link to post
Share on other sites
Phoresis    122
Quote:
Original post by Simian Man
The types TableComponent* and vector<TableComponent>::iterator are not compatible. Just use the iterator type instead - it can do everything a pointer can.

vector<TableComponent>::iterator t;

for(vector<TableComponent>::iterator iter = items.begin(); iter != items.end(); iter++)
{
if (test == iter->getId())
{
t = iter;
}
}


HTH


Ah right, thanks, that makes sense now.

Share this post


Link to post
Share on other sites
AshleysBrain    162
Don't forget you can take the address of the object pointed to by an iterator like so:

&*iter


'iter' used on its own is a class, which cannot be cast directly to a T*.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this