Recently, I wanted to go from raw pointers to smart pointers in my code (note that it used to work without any problems with raw pointers).
Now, I have a problem with the copy constructor.
Let say that I want to create a new Frame shared_ptr like this (with f1
being a std::shared_ptr<Frame>
object):
std::shared_ptr<Frame> global_frame_1 = std::make_shared<Frame>(f1->duplicateFrame());
The copy constructor of the Frame
object is defined as follows:
Frame::Frame(const Frame& frame) {
m_joints = frame.m_joints;
}
It then gives me the following error, in the file where the first line of code I provided is located:
C2664 'Frame::Frame(const Frame &)' : cannot convert parameter 1 from '_Ty' to 'const std::shared_ptr<Frame>'
Please note that since the compiler doesn't point me to a specific line in my code (but rather a line in the xmemory
file), I can only guess, after looking at the specified line in the file, that these kind of assignment are the problem I'm facing.
I first thought that I needed a copy constructor that would take a std::shared_ptr<Frame>
, so I implemented one:
Frame::Frame(const std::shared_ptr<Frame> frame) {
m_joints = frame->m_joints;
}
But now it gives me the same error in the Frame
file.
It seems that I don't fully understand either how constructor copy works, or how shared_ptr works (probably both). Could someone give me a hint about what's wrong and how could I fix this?
Cheers!
So, as Kevin pointed it in the comments: I didn't need to do the whole std::shared_ptr<Frame> global_frame_1 = std::make_shared<Frame>(f1->duplicateFrame())
thing, since I modified the duplicateFrame()
method to already return a shared pointer. Replacing it with std::shared_ptr<Frame> global_frame_1 = f1->duplicateFrame();
did the trick. Thanks a lot!