-
October 29th, 2011, 02:23 PM
#1
vector iterator
Code:
Product Store::getProduct(int index)
{
int indexim=0;
vector<Product>::iterator it;
for ( it=(this->availableProducts.begin()) ; it < (this->availableProducts.end()); it++,indexim++ )
{
if(index == indexim)
{
return *it;//error is here. 'return it' doesn't work too.
}
}
}
I want to return Product class.When i compile it , i'm taking big errors.Why? What should i do?
-
October 29th, 2011, 02:38 PM
#2
Re: vector iterator
Firstly, when iterating, you should use:
Code:
it=(this->availableProducts.begin()) ; it != (this->availableProducts.end()); it++,indexim++ )
Nest, why not simply:
Code:
Product Store::getProduct(int index)
{
return availableProducts[index];
}
It does the same thing but in a not so convoluted way.
-
October 29th, 2011, 02:43 PM
#3
Re: vector iterator
It should work.. What errors do you get?
By the way:
- Normally you use it != this->availableProducts.end()
- You could drop the this-> since you access a member variable
-
October 29th, 2011, 02:45 PM
#4
Re: vector iterator
Good suggestion Chris but that should also incorprate a check that index is not outside availableProducts
-
October 29th, 2011, 03:44 PM
#5
Re: vector iterator
i spend one hour for it . But i forgot a small point i found thanks to God. I forgot including Product class. After including, it worked. Thanks for your interest
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|