c++ STL向量不接受复制构造函数

发布于 2024-09-03 00:48:45 字数 677 浏览 2 评论 0原文

我编写了一个代码(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七分※倦醒 2024-09-10 00:48:45

复制构造函数应该将对象作为 const 引用,所以它应该是:

A(const A &a){ cout << "Copy Constructor of A is acting" << endl; }

Copy constructor should take the object as a const reference, so it should be:

A(const A &a){ cout << "Copy Constructor of A is acting" << endl; }
仅一夜美梦 2024-09-10 00:48:45

认为复制构造函数采用 const ref 的

尝试

A(const A &a) { cout << "Copy Constructor of A is acting" << endl ; } 

希望有帮助

Think copy constructors take const ref's

try

A(const A &a) { cout << "Copy Constructor of A is acting" << endl ; } 

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文