CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: vector iterator

  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    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?

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    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.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: vector iterator

    Good suggestion Chris but that should also incorprate a check that index is not outside availableProducts
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    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
  •  





Click Here to Expand Forum to Full Width

Featured