Can list container contain an vector object, such as list< vector<string> >??

Discussion in 'C++' started by ehui928, May 29, 2006.

  1. ehui928

    ehui928 Guest

    hi, everybody
    I am a newbie in STL. When I compile the following program under
    gcc4.0, I got a the following errors.
    I wonder whether the form of list< vector<string> > is correct in STL
    ?
    // test.cpp
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <list>

    using namespace std;

    typedef vector<string> RECORD;
    typedef list< RECORD > DataInstances;

    DataInstances getRecords(const string& filename);
    void print_data(const DataInstances& d);
    void print_vector(const vector<string>& s);

    const string filename = "mydata";

    int main(void)
    {
    DataInstances data;

    data = getRecords(filename);
    print_data(data);

    return 0;
    }

    DataInstances getRecords(const string& filename)
    {
    .......
    return instances;

    }
    void print_vector(const vector<string>& s)
    {
    ......
    }

    void print_data(const DataInstances& d)
    {
    DataInstances::iterator pos;
    // the compiler seems to say "pos = d.begin()" is error? why
    ?
    for (pos = d.begin(); pos != d.end(); ++pos) { //test.cpp:86
    ,complier say this line has error
    print_vector(*pos);
    }
    }

    Following is the compile errors:

    In function 'void print_data(const DataInstances&)':
    test.cpp:86: error: no match for 'operator=' in 'pos = (+
    d)->std::list<_Tp, _Alloc>::begin [with _Tp =
    std::vector<std::basic_string<char, std::char_traits<char>,
    std::allocator<char> >, std::allocator<std::basic_string<char,
    std::char_traits<char>, std::allocator<char> > > >, _Alloc =
    std::allocator<std::vector<std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >,
    std::allocator<std::basic_string<char, std::char_traits<char>,
    std::allocator<char> > > > >]()'
    /usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_list.h:112:
    note: candidates are:
    std::_List_iterator<std::vector<std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >,
    std::allocator<std::basic_string<char, std::char_traits<char>,
    std::allocator<char> > > > >&
    std::_List_iterator<std::vector<std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >,
    std::allocator<std::basic_string<char, std::char_traits<char>,
    std::allocator<char> > > > >::eek:perator=(const
    std::_List_iterator<std::vector<std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >,
    std::allocator<std::basic_string<char, std::char_traits<char>,
    std::allocator<char> > > > >&)


    Any one give me some help ?
    Thanks!
     
    ehui928, May 29, 2006
    #1
    1. Advertising

  2. ehui928

    Kai-Uwe Bux Guest

    ehui928 wrote:

    > hi, everybody
    > I am a newbie in STL. When I compile the following program under
    > gcc4.0, I got a the following errors.
    > I wonder whether the form of list< vector<string> > is correct in STL
    > ?


    It is.

    > // test.cpp
    > #include <iostream>

    [snip]
    >
    > void print_data(const DataInstances& d)
    > {
    > DataInstances::iterator pos;
    > // the compiler seems to say "pos = d.begin()" is error? why
    > ?


    You try to obtain an iterator to a const container. Try const_iterator.

    > for (pos = d.begin(); pos != d.end(); ++pos) { //test.cpp:86
    > ,complier say this line has error
    > print_vector(*pos);
    > }
    > }

    [snip]


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, May 29, 2006
    #2
    1. Advertising

  3. ehui928

    ehui928 Guest

    OK, now it works!
    Thank you very much!
     
    ehui928, May 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jess
    Replies:
    6
    Views:
    285
    blangela
    May 9, 2007
  2. puzzlecracker
    Replies:
    4
    Views:
    346
    Hendrik Schober
    Oct 14, 2008
  3. Extreme Noob
    Replies:
    4
    Views:
    150
    7stud --
    May 11, 2008
  4. Roger Pack
    Replies:
    3
    Views:
    162
    Roger Pack
    Sep 28, 2010
  5. Jason Carlton
    Replies:
    11
    Views:
    250
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page