关于vector使用编译的一个有关问题
关于vector使用编译的一个问题代码:C/C++ codeclass base{public:int abase(int temp){cout sadf
关于vector使用编译的一个问题
代码:
C/C++ codeclass base{public: int a; base(int temp) { cout << "sadf" <<endl; a = temp; } base(base &hello) { cout << "base" <<endl; }};int main(){#if 1 vector<base> vec; for (int i = 0; i < 10; i++) { vec.push_back(base(i*2)); }#endif}
编译错误:
c:\program files\microsoft visual studio 8\vc\include\vector(1125) : error C2558: class 'base' : no copy constructor available or copy constructor is declared 'explicit'
1> c:\program files\microsoft visual studio 8\vc\include\vector(1117) : while compiling class template member function 'void std::vector<_Ty>::_Insert_n(std::_Vector_iterator<_Ty,_Alloc>,__w64 unsigned int,const _Ty &)'
1> with
1> [
1> _Ty=base,
1> _Alloc=std::allocator<base>
1> ]
1> c:\users\iceking\desktop\test\cjf\test.cpp(594) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=base
1> ]
C++水平实在一般,拷贝构造函数 参数添加const就可以,我想知道为什么呢,解释下。。。。
[解决办法]改为如下的,因为vector在insert的时候,需要const的引用。
base(const base &hello)
{
cout << "base" <<endl;
}
[解决办法]谁能解释一下输出:
sadf
base
base
sadf
base
base
base
sadf
base
base
base
base
。。。。
每一次循环多一个base