I have a class called classA
, something like this:
class classA {
private:
char* data;
public:
classA(const classA&) = delete;
~classA();
};
~classA()
{
delete[] data;
}
In another class, let's call it classB
, I have as a member a shared pointer to classA
:
class classB
{
private:
std::shared_ptr<classA> ptrA;
public:
classB(std::shared_ptr<classA>);
};
classB(std::shared_ptr<classA> sp) : ptrA(sp)
{}
This is how I instantiate my classB
:
classA ca;
classB cb(std::make_shared<classA>(ca));
This gives me the following error:
attempting to reference a deleted function
Obviously, I am trying to reference the copy constructor, which I defined as deleted
(there is a reason for this, objects of this class shouldn't be copied). But I am confused as to why the copy constructor is called since I am passing a shared pointer, and how to avoid this.
std::make_shared<classA>(std::move(ca))
, you'd still have a problem, see Default move constructor/assignment and deleted copy constructor/assignment – Goldiamake_shared
is trying to construct aclassA
from anotherclassA
(ca
). What are you trying to do? Are you hoping for theshared_ptr
to take ownership ofca
? – Flashbulbca
is not an instance ofclassA
– ClclassA ca();
forward declares a function calledca
that returns aclassA
, I think that we all know what the OP really did mean. Sure, they didn't post actual code, but it's still clear enough – GoldiaclassA
otherwise I'm not sure how they'd come across this error (likely they'd have had a different one) – Flashbulb