C ++ STL vector does not accept copy constructor

I wrote code (C ++, visual studio 2010) that has a vector, even if I declare that const const is declared, but still shows that copy const is not declared

Here is 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 received is

Error 1 error C2558: class 'A': no โ€‹โ€‹copy constructor is available or the copy constructor is declared "explicit" c: \ program files \ microsoft visual studio 10.0 \ vc \ include \ xmemory 48 1 delete

Someone please help me

+4
source share
2 answers

The copy constructor should take an object as a reference to a constant, so it should be:

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

Think copy constructors accept const ref

try

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

Hope that helps

+5
source

Source: https://habr.com/ru/post/1311224/


All Articles