H
hweekuan
hi,
it seems i can't assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.
summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member 'const unsigned int A::u',
can't use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method 'A& A:perator=(const A&)' first required here
code:
-------
1
2 #include <vector>
3
4 struct A {
5 A(const unsigned int a) : u(a) { }
6 private: const unsigned int u;
7 };
8
9 int main () {
10
11 std::vector<A> y;
12 y.push_back(A(2));
13 }
full compile error:
-------------------------
[16:34]:Macintosh:tmp 7+ g++ cpp.C
cpp.C: In member function 'A& A:perator=(const A&)':
cpp.C:4: instantiated from 'void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename
_Alloc:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]'
/usr/include/c++/4.0.0/bits/stl_vector.h:610: instantiated from
'void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = A,
_Alloc = std::allocator<A>]'
cpp.C:12: instantiated from here
cpp.C:4: error: non-static const member 'const unsigned int A::u',
can't use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc: In member function 'void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename
_Alloc:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]':
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method 'A& A:perator=(const A&)' first required here
it seems i can't assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.
summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member 'const unsigned int A::u',
can't use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method 'A& A:perator=(const A&)' first required here
code:
-------
1
2 #include <vector>
3
4 struct A {
5 A(const unsigned int a) : u(a) { }
6 private: const unsigned int u;
7 };
8
9 int main () {
10
11 std::vector<A> y;
12 y.push_back(A(2));
13 }
full compile error:
-------------------------
[16:34]:Macintosh:tmp 7+ g++ cpp.C
cpp.C: In member function 'A& A:perator=(const A&)':
cpp.C:4: instantiated from 'void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename
_Alloc:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]'
/usr/include/c++/4.0.0/bits/stl_vector.h:610: instantiated from
'void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = A,
_Alloc = std::allocator<A>]'
cpp.C:12: instantiated from here
cpp.C:4: error: non-static const member 'const unsigned int A::u',
can't use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc: In member function 'void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename
_Alloc:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]':
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method 'A& A:perator=(const A&)' first required here