469,889 Members | 1,258 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

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

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> > > > >::operator=(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!

May 29 '06 #1
2 2346
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
May 29 '06 #2
OK, now it works!
Thank you very much!

May 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.