简体   繁体   English

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

[英]c++ STL vector is not acccepting the copy constructor

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 我写了一个代码(c ++,visual studio 2010),它有一个向量,即使我虽然声明了复制const,但仍然显示未声明复制const

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 错误1错误C2558:类'A':没有可用的复制构造函数或复制构造函数被声明为'显式'c:\\ program files \\ microsoft visual studio 10.0 \\ vc \\ include \\ xmemory 48 1删除

Anybody please help me 有人请帮帮我

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

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

Think copy constructors take const ref's 认为复制构造函数需要const ref

try 尝试

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

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM