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
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; }
Think copy constructors take const ref's
try
A(const A &a) { cout << "Copy Constructor of A is acting" << endl ; }
Hope that helps
精彩评论