Following program looks pretty OK to me. But I can not get it compiled.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
struct a
{
int i;
int j;
};
std::vector<a*> vecA;
a* pA = new a;
pA->i = 4;
pA->j = 9;
vecA.push_back(pA);
return 0;
}
It generates following error.
struct_update.cc: In function ‘int main()’:
struct_update.cc:32:19: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::a*’
struct_update.cc:32:19: error: trying to instantiate ‘template<class _Alloc> class std::allocator’
struct_update.cc:32:19: error: template argument 2 is invalid
struct_update.cc:32:25: error: invalid type in declaration before ‘;’ token
struct_update.cc:39:10: error: request for member ‘push_back’ in ‘vecA’, which is of non-class type ‘int’
This isn’t true anymore in the new C++11 standard, but current compilers don’t fully implement it yet.
A local type can’t be a template parameter. Move your structure definition above
main, and everything will work.Or update your compiler to one that supports this part of C++11.
Here’s the restriction from C++03 section 14.3.1 (
[temp.arg.type]), which is removed in C++11: