Re: vector<struct> cannot be compiled under gcc
"fifth8118" <fifth8118@gmail.com> skrev i meddelandet
news:1151418706.493978.94500@b68g2000cwa.googlegroups.com...
Hi, all:
I wrote following code, and compile it under gcc 3.4.3, but cannot
be
compiled.
#include <vector>
#include <string>
using namespace std;
int main() {
struct stut {
float a;
string b;
};
vector <stut> d;
return 1;
}
The Error message is:
temp.cpp: In function `int main()':
temp.cpp:13: error: `main()::stut' uses local type `main()::stut'
temp.cpp:13: error: trying to instantiate `template<class _Alloc>
class std::allocator'
temp.cpp:13: error: template argument 2 is invalid
temp.cpp:13: error: invalid type in declaration before ';' token
can somebody point out what's wrong there? Thanks.
You cannot instantiate a template on a local type, like stut. Move it
outside of main, and it will work.
The problem is that if you have several functions with equally named
local types, that could cause confusion.
void f1()
{
struct stut {
int x;
};
vector<stut> v1; // illegal code
}
void f2()
{
struct stut {
float y;
};
vector<stut> v2; // illegal code
}
Now v1 and v2 are both of type vector<stut>, but still different. Then
what?
The standards committee solved the problem by stating that it is just
not allowed!
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]