<list>

This is a discussion on <list> within the C++ Programming forums, part of the General Programming Boards category; I made a class and store pointers to instances of these classes in a linked list ( <list> using namespace ...

  1. #1
    Unregistered
    Guest

    <list>

    I made a class and store pointers to instances of these classes in a linked list ( <list> using namespace std; )

    how can i call one of the instances 's functions from the list.

    looking for something like

    list <MYCLASS *> MyList;

    MyList[i]->ShowMethod(); <------- ????????

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to use an iterator. Something like -

    list <MYCLASS *>::iterator it = MyList.begin();
    (*it)->ShowMethod();

    lists aren't random access containers so don't overload the subscript operators. If you wish to iterate through the list operators -- and ++ are defined for the iterator.

  3. #3
    Unregistered
    Guest
    Sorensen4prez !!


    ..Thanks

  4. #4
    Unregistered
    Guest

    new Error

    error C2143: syntax error : missing ';' before '<'


    class TheMain{
    private:

    list <Mclass*> Mlist;

    public:
    .....
    };


    what's wrong with that ?

  5. #5
    Unregistered
    Guest
    if I
    #include <list>
    using namespace std;

    in the header file i get like 106 errors, besides it's defined in the .cpp

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You should include <list> in the header where your class is declared (which should be included in the cpp file that defines your member functions), and you should use -

    using namespace std;

    in header files, as it forces std namespace into global scope for anyone that uses your headers whether they want it there or not. Use

    using std::list

    if you can't be bothered with per-fixing std to everything. What are some of the 106 errors that you get?

  7. #7
    Unregistered
    Guest
    Errors:

    error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(159) : error c:\microsoft visual studio\vc98\include\streamb.h(90) : error C2872: 'streamoff' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\streamb.h(91) : error C2872: 'streampos' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\streamb.h(91) : error C2872: 'streampos' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(258) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(258) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(259) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(259) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(260) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(260) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(272) : error C2872: 'ostream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(272) : error C2872: 'ostream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(272) : error C2872: 'ostream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(273) : error C2872: 'ostream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\ios.h(290) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(67) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(74) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(74) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(90) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(102) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(117) : error C2872: 'streampos' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(118) : error C2872: 'streamoff' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(119) : error C2872: 'streampos' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(126) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(132) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(139) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(140) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(140) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(140) : error C2872: 'ios' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(142) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(143) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(145) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(146) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(148) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(149) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(150) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(152) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(153) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(155) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(156) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(157) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(159) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(161) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(162) : error C2872: 'istream' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(167) : error C2872: 'streambuf' : ambiguous symbol
    c:\microsoft visual studio\vc98\include\istream.h(169) : error C2872: 'istream' : ambiguous symbol

  8. #8
    Unregistered
    Guest
    Top of my headername.h
    ---------------------------------
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;

    top of .cpp
    ----------------------------------
    #include "headername.h"

  9. #9
    Unregistered
    Guest
    If I put

    in the .cpp
    --------------------------
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;

    #include "headername.h"
    ----------------------------------

    I get these errors :

    c:\a2-list\form.h(11) : error C2143: syntax error : missing ';' before '<'
    c:\a2-list\form.h(11) : error C2501: 'list' : missing storage-class or type specifiers
    c:\a2-list\form.h(11) : error C2059: syntax error : '<'
    c:\a2-list\form.h(11) : error C2238: unexpected token(s) preceding ';'



    any ideas in either case?

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The std includes want to be in a header file, not the cpp file. Normally you get the ambigious symbol errors messages when you try to mix old and new style headers. Make sure you haven't got <iostream.h> included anywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <list> iterators with nested templates
    By dwylie in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2005, 12:13 PM
  2. I am back with another <list> question....
    By chadsxe in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 12:53 PM
  3. Using <list>
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 06-08-2004, 08:04 AM
  4. dynamic objects in a <list>
    By strobe9 in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 05:50 PM

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