Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 314
prabhakaran Avatar asked May 28 '10 16:05

prabhakaran


2 Answers

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; }
like image 124
reko_t Avatar answered Oct 24 '22 19:10

reko_t


Think copy constructors take const ref's

try

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

Hope that helps

like image 42
Omas Illomitzer Avatar answered Oct 24 '22 19:10

Omas Illomitzer