Thread: accessing objects in my list

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    accessing objects in my list

    Code:
    int main()
    {
    
    	list<item> itemList;
    
    	itemList.push_back(item( "font", "qwer" ));
    	itemList.push_back(item( "2", "qwer" ));
    	itemList.push_back(item( "3", "qwer" ));
    	itemList.push_back(item( "4", "qwer" ));
    	itemList.push_back(item( "back", "qwer" ));
    
    	list<item>::iterator iter;
    	iter = itemList.begin();
    
    	while ( iter != itemList.end() )
    	{
    		cout << *iter;
    		//cout << *iter.getProductID(); //this wont work
    		iter++;
    	}
    for a progamming assignment ive created a list that contains a growing number of objects. using an itorator to browse threw the list im able to actually cout the item objects because i overloaded the << operator.

    unfortunatly i have no idea how i would now gain access to the rest of the functions that my iter is on. if i uncomment the line above heres what i get.

    Code:
    error C2039: 'getProductID' : is not a member of 'iterator'
    i know that technically iter is just a pointer to my objects, but if i dereference it then why wouldnt it be able to use it owns functions???

    thanks for any help that somebody might provide. ill be watching this topic closely.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You need to mind your order of operations. The dot operator binds more tightly than dereferencing. You need to use (*iter).getProductID(). Or what is essentially its synonym: iter->getProductID().

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    ya know...

    i knew it'd be something like this. my teacher explained something like this as well. basically the -> operator says iter poitns to what ever type it is an can access the data right?

    still though it makes me wonder just what was exactly going on with my code since it use the dot (?) operator first.

    thanks for such a quick response. im sure ill run into other trouble soon enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  2. Linked List Class with Pointers to Objects and using Polymorphism
    By CaptainMorgan in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 11:41 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM