c++ STL向量不接受复制构造函数
我编写了一个代码(c ++,Visual Studio 2010),它有一个向量,即使我虽然声明了copy const,但仍然显示未声明copy const
这里的代码
#include<iostream>
#include<vector>
using namespace std;
class A
{
public:
A() { cout << "Default A is acting" << endl ; }
A(A &a) { cout << "Copy Constructor of A is acting" << endl ; }
};
int main()
{
A a;
A b=a;
vector<A> nothing;
nothing.push_back(a);
int n;
cin >> n;
}
我得到的错误是
错误 1 错误 C2558:类“A”:没有可用的复制构造函数或复制构造函数被声明为“显式”c:\program files\microsoft Visual Studio 10.0\vc\include\xmemory 48 1 删除
任何人请帮助我
I wrote a code ( c++,visual studio 2010) which is having a vector, even I though copy const is declared, but is still showing that copy const is not declared
Here the code
#include<iostream>
#include<vector>
using namespace std;
class A
{
public:
A() { cout << "Default A is acting" << endl ; }
A(A &a) { cout << "Copy Constructor of A is acting" << endl ; }
};
int main()
{
A a;
A b=a;
vector<A> nothing;
nothing.push_back(a);
int n;
cin >> n;
}
The error I got is
Error 1 error C2558: class 'A' : no copy constructor available or copy constructor is declared 'explicit' c:\program files\microsoft visual studio 10.0\vc\include\xmemory 48 1 delete
Anybody please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
复制构造函数应该将对象作为 const 引用,所以它应该是:
Copy constructor should take the object as a const reference, so it should be:
认为复制构造函数采用 const ref 的
尝试
希望有帮助
Think copy constructors take const ref's
try
Hope that helps