c++ - Vector of custom objects : Assign compilation failure -
i creating vector of custom objects , calling assign follows:
class myclass { public: myclass() { cout<<"myclass def const"<<endl; } myclass(const myclass &mclass) {cout<<"default const"<<endl;} myclass& operator=(myclass &mclass) { cout<<"called overloaded = operator"<<endl; return mclass; } }; int main() { myclass m; cout<<"orginal object:"<<endl; vector<myclass> vec1,vec2,vec3; vec1.assign(10,m); return 0; }
relevant compilation errors:
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_vector.h:344: instantiated ‘void std::vector<_tp, _alloc>::assign(size_t, const _tp&) [with _tp = myclass, _alloc = std::allocator<myclass>]’ test.cpp:52: instantiated here /usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_algobase.h:686: error: no match ‘operator=’ in ‘* __first = __value’ test.cpp:43: note: candidates are: myclass& myclass::operator=(myclass&)
i not sure missing since have overloaded = operator.
edit :
it seems signature wrong corrected below. understand valid signatures are:
(1) myclass& operator=( const myclass& rhs ); (2) myclass& operator=( myclass& rhs ); (3) myclass& operator=( myclass rhs ); (4) const myclass& operator=( const myclass& rhs ); (5) const myclass& operator=( myclass& rhs ); (6) const myclass& operator=( myclass rhs ); (7) myclass operator=( const myclass& rhs ); (8) myclass operator=( myclass& rhs ); (9) myclass operator=( myclass rhs );
where restriction pass argument const reference defined?
the argument assignment operator must const
, e.g.
myclass& operator=(const myclass &mclass)
Comments
Post a Comment