list of classes

This is a discussion on list of classes within the C++ Programming forums, part of the General Programming Boards category; In my program, I implement a list of classes using STL list. However, if I try calling a method of ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    39

    list of classes

    In my program, I implement a list of classes using STL list. However, if I try calling a method of a class that the iterator points to, the compiler gives me the error "request for member of non-aggregate type before ')' token". What is wrong? The code is approximately like this:

    Code:
    list<class class_name*> lst
    list<class class_name*>::iterator it;
    it = lst.begin();
    it->function();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Notice that your doubly linked list stores pointers to class_name objects, not class_name objects themselves. By the way, the class keyword is unnecessary here, i.e., you can write list<class_name*>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    39
    Quote Originally Posted by laserlight View Post
    Notice that your doubly linked list stores pointers to class_name objects, not class_name objects themselves. By the way, the class keyword is unnecessary here, i.e., you can write list<class_name*>.
    So, what do I need to change?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    You can either use a list<class_name>, which could mean changing other code, or you dereference the iterator to get the pointer, and then call the function using the pointer, e.g.,
    Code:
    (*it)->function();
    If you can use the former, it is likely to be the better solution overall.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    39

    Post

    Quote Originally Posted by laserlight View Post
    You can either use a list<class_name>, which could mean changing other code, or you dereference the iterator to get the pointer, and then call the function using the pointer, e.g.,
    Code:
    (*it)->function();
    If you can use the former, it is likely to be the better solution overall.
    That was it! Thank you so much!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Don't forget to clean up since you're dealing with raw pointers here!
    Or better yet, use a smart pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 03:58 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 05:41 AM

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