Thread: list of classes

  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
    28,413
    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*>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 02: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, 10: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