• Advertisement
Sign in to follow this  

C++ Vectors

This topic is 3978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Moved to For Beginners.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement