如果是這樣,爲什麼?爲什麼不使用值類型的複製構造函數?std :: vector是否將其值類型的賦值運算符用於push_back元素?
我得到以下錯誤:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio
n `ClassWithoutAss& ClassWithoutAss::operator=(const ClassWithoutAss&)':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: instantiate
d from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato
r<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
ClassWithoutAss, _Alloc = std::allocator<ClassWithoutAss>]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:564: instantia
ted from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Class
WithoutAss, _Alloc = std::allocator<ClassWithoutAss>]'
main.cpp:13: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: error: non-st
atic const member `const int ClassWithoutAss::mem', can't use default assignment
operator
運行g。在下面的代碼++ main.cpp中:
/*
* ClassWithoutAss.h
*
*/
#ifndef CLASSWITHOUTASS_H_
#define CLASSWITHOUTASS_H_
class ClassWithoutAss
{
public:
const int mem;
ClassWithoutAss(int mem):mem(mem){}
ClassWithoutAss(const ClassWithoutAss& tobeCopied):mem(tobeCopied.mem){}
~ClassWithoutAss(){}
};
#endif /* CLASSWITHOUTASS_H_ */
/*
* main.cpp
*
*/
#include "ClassWithoutAss.h"
#include <vector>
int main()
{
std::vector<ClassWithoutAss> vec;
ClassWithoutAss classWithoutAss(1);
(vec.push_back)(classWithoutAss);
return 0;
}
如果僅用於'ClassWithoutAss',則爲+1。 – GManNickG 2010-07-17 18:28:35
爲什麼你將括號vec.push_back ...它不會引起任何問題,但似乎有點不必要... – Goz 2010-07-17 18:56:51
這是一個「驢」在「驢」? – sbi 2010-07-17 21:22:01