views:

168

answers:

2

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

+11  A: 

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; }
reko_t
@reko_t Thank you, it works
prabhakaran
+3  A: 

Hi,

Think copy constructors take const ref's

try

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

Hope that helps

Omas Illomitzer
@willomitzerThank you, it works
prabhakaran