|
From: Oscar F. <of...@wa...> - 2002-09-26 20:18:40
|
"Tommy Ngo" <pn...@vi...> writes:
> I am having problems compiling
> the code below with Mingw32 (GCC v3.2).
> Can you please help me with this?
>
> Thanks,
>
> Tommy.
>
>
> --------------------------------------------
> #include<iostream>
> #include<vector>
>
> using namespace std;
>
> struct Data {
> vector<string> x;
> };
>
>
> int main(void) {
>
> vector<Data> vec(10);
>
> vec[0].x.insert(&vec[0].x[0], "data");
>
> cout << vec[0].x[0] << endl;
>
> return 0;
>
> }
> --------------------------------------------
>
>
> Here is the error message:
>
> >Error Message:
> >--------------
> >
> >insert.cc: In function `int main()':
> >insert.cc:15: no matching function for call to `std::vector<std::string,
> > std::allocator<std::string> >::insert(std::string*, const
> >char[5])'
[snip]
Esa Vuokko explained the problem on this mailing list a few hours ago:
--- Quote
&y[0].x[0] returns a pointer to std::string.
vector::insert expects vector::iterator.
libstdc++v2 used pointers as iterators, while
libstdc++v3 uses classes with neccesary operators
defined.
To get corresponding iterator in this case, you can use
y[0].x.begin()
or in general (for random access containers)
y[0].x.begin()+n
where n is zero-based index.
--- End quote.
[snip]
> -----Original Message-----
> From: Bjarne Stroustrup [mailto:bs...@re...]
> Sent: Thursday, September 26, 2002 11:59 AM
> To: pn...@vi...
> Subject: Re: Compiling Problems with insert().
>
>
> Correction: The compiler I used wasn't GCC 3.2. With GCC 3.2 I get
> the same error as you do. With EDG on SGI, your program compiles and
> runs as expected, giving the output "data"
I'm afraid B.S. is too busy to *think* about every request for
technical support people throws at him <g>
[snip]
--
Oscar
|