in the following code:
class prova: public std::map< int,int >
{
public:
prova():a(5){}
int a;
};
int main()
{
std::vector<prova> p;
prova obj;
p.push_back(obj);
return 0;
}
it works without problem; If I add a reference member
class prova: public std::map< int,int >
{
public:
prova():a(5){}
int a;
int& b=a;
};
int main()
{
std::vector<prova> p;
prova obj;
p.push_back(obj);
return 0;
}
I have
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
/home/andrea/Scaricati/prova.cpp||In instantiation of ‘void
std::vector<_Tp,_Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&)
[with _Tp = prova; _Alloc = std::allocator<prova>;
std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<prova*, std::vector<prova> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = prova*]’:|
/usr/include/c++/4.8/bits/stl_vector.h|913|required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = prova; _Alloc = std::allocator<prova>; std::vector<_Tp, _Alloc>::value_type = prova]’|
/home/andrea/Scaricati/prova.cpp|19|required from here|
/home/andrea/Scaricati/prova.cpp|4|error: non-static reference member ‘int& prova::b’, can’t use default assignment operator|
/usr/include/c++/4.8/bits/vector.tcc|335|note: synthesized method ‘prova& prova::operator=(const prova&)’ first required here
|||=== Build failed: 1 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
no problems adding -std=g++11 like suggested from warning. If I remove the vector declaration I have just the warning without the error. Why C++ old standard can't do it? what is the problem with references attributes? is there a system to do same things without using c++11 standard? I try
class prova: public std::map< int,int >
{
public:
prova():a(5),b(a){}
int a;
int &b;
};
but I get errors. Why this? thanks!