problems accessing a structlist

This is a discussion on problems accessing a structlist within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm trying to access a list directly by using [position] on the listname. Here is the code: Code: typedef ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    30

    problems accessing a structlist

    Hi, i'm trying to access a list directly by using [position] on the listname.

    Here is the code:

    Code:
    typedef struct {
    	double dx;
    	double dy;
    	int ic;
    } t_fingertip;
    
    typedef struct{
    	double dd;
    	int ic;
    	int ix;
    	int iy;
    }t_knnclassify;
    
    typedef struct{
    	int ix;
    	int iy;
    	int ic;
    } t_knnclassified;
    
    typedef list<t_knnclassify> CLASSIFYLIST;
    typedef list <t_knnclassified> CLASSIFIEDFLIST;
    typedef list<t_fingertip> FINGERLIST;
    
    CLASSIFIEDFLIST final;
    //filled final with some elements
    //create an element with data from final-list for the flists (of type FINGERLIST)
    //ipos is var given by methoddeclaration
    t_fingertip ftx = {(int)final[ipos].ix, (int)final[ipos].iy, (int)final[ipos].ic};
    //Adding to list
    ptLocalVars->flists->push_back(ftx);

    When i try to compile it i get this error:

    fingerClassifier.cc:809: no match for `CLASSIFIEDFLIST &[int &]'
    fingerClassifier.cc:809: no match for `CLASSIFIEDFLIST &[int &]'
    fingerClassifier.cc:809: no match for `CLASSIFIEDFLIST &[int &]'


    Anybody knows how to fix it?

    Thanks in advance!

    ~jan

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    std::list does not overload the subscript operator [].

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    so how can i access the data?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you don't NEED to use an STL list, you could use an STL vector, and then you can use the [] operator to access the data. If you HAVE to use an STL list, then I would try a list iterator assigned to the beginning of the list and walk it down ipos number of nodes or you could walk it down until you came to a node with a particular value(s) or you could try using find() to find the first node in the list with a desired value(s).

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    If you want to use subscripts, something like this should do the trick:
    Code:
    template <typename T>
    const T& ListPosition(std::list<T>::size_type Position, const std::list<T>& List)
    {
             std::list<T>::const_iterator ListIter;
             for (std::list<T>::size_type i = 0; i < Position; ++i)
             {
    		++ListIter;
             }
             return *ListIter;
    }
    I didn't test it, so there's bound to be an error or two.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, Eibro, that sort of defeats the whole reason there is no [] operator already -- it's very inefficient. Only vector and deque allow random access; list does not. That's because only for vector or deque is it efficient to find, say, the 234th element of the collection.

    List is bad at accessing elements in random order (which is what the subscript does) and is good at iterating through the list (e.g. traverse the elements first to last). If you want guaranteed constant time insertion at any arbitrary location, list is good, but don't try to access a single element in the middle.
    Last edited by Cat; 07-17-2003 at 12:32 AM.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Cat
    Well, Eibro, that sort of defeats the whole reason there is no [] operator already -- it's very inefficient. Only vector and deque allow random access; list does not. That's because only for vector or deque is it efficient to find, say, the 234th element of the collection.

    List is bad at accessing elements in random order (which is what the subscript does) and is good at iterating through the list (e.g. traverse the elements first to last). If you want guaranteed constant time insertion at any arbitrary location, list is good, but don't try to access a single element in the middle.
    Yes, I realize this. I'd never use something like that, but perhaps he has dug himself a hole and this is the easiest way out. I guess I should have posted a warning with that code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 10:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21