Jump to content

  • Log In with Google      Sign In   
  • Create Account

std::vector trouble


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 20 January 2013 - 08:15 PM

Hello, what am i doing wrong in this code:

for(int i = 0; i != m_Inventory.end(); ++i){
 
    //inventory crap
}
 

 

the problem is in

i != m_Inventory.end();

 

It says that the != operator is unknown. Now i believe this is saying i need to overload the != operator, but why? Thanks for all help.



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9595

Like
3Likes
Like

Posted 20 January 2013 - 08:17 PM

You're mixing up iterators and integers. If you want to loop over indexes you need to use i != m_Inventory.size(), if you want to loop over iterators you need to use std::vector<whatever>::iterator i = m_Inventory.begin() (or the equivalent such as C++11's auto).

#3 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 20 January 2013 - 08:20 PM

Thanks :) 



#4 Servant of the Lord   Crossbones+   -  Reputation: 19520

Like
0Likes
Like

Posted 20 January 2013 - 10:38 PM

And if you are using C++11, not only can you use auto like SiCrane mentioned, you can reformat the entire for() loop into a range-style for() loop:

for(const InventoryItem &item : m_Inventory) //Note the single 'colon' instead of two 'semicolons'.
{
 
}

(You can also use 'auto' when specifying the element type in the range-for)

 

It loops over the entire container, and de-references the iterator for you.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Washu   Senior Moderators   -  Reputation: 5179

Like
2Likes
Like

Posted 21 January 2013 - 01:33 AM

And if you are using C++11, not only can you use auto like SiCrane mentioned, you can reformat the entire for() loop into a range-style for() loop:

 

for(const InventoryItem &item : m_Inventory) //Note the single 'colon' instead of two 'semicolons'.
{
 
}

 

(You can also use 'auto' when specifying the element type in the range-for)

 

It loops over the entire container, and de-references the iterator for you.

 

 

for(auto const& item : inventory) {
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#6 Servant of the Lord   Crossbones+   -  Reputation: 19520

Like
1Likes
Like

Posted 21 January 2013 - 10:50 AM

When introducing range-for the first time, I use the explicit type to show that range-for doesn't depend on 'auto', and to illustrate what the range-for is actually doing.

Almost all of my actual uses of range-for() use auto - but I try not to bootstrap too many new concepts together in one snippet or it makes the individual and distinct concepts harder to learn. People don't have to learn what 'auto' means before they can use range-for(). wink.png


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS