Hey guys, I am developing my game engine and got stuck trying to implement shared pointers in some of my classes.
That is the situation: I am trying to pass a CONST REFERENCE shared pointer to a class method and inside of that method I want to assign this pointer as member of that class. This is the code:
// Header
class OpenGLVertexArray : public IVertexArray
{
private:
// …
std::shared_ptr<IVertexBuffer> m_IndexBuffer; // The member variable that I want to assign…
// …
public:
// …
virtual void SetIndexBuffer(const std::shared_ptr<IIndexBuffer>& indexBuffer) override; // method declaration
//…
};
// Source
void OpenGLVertexArray::SetIndexBuffer(const std::shared_ptr<IIndexBuffer>& indexBuffer)
{
// … do some openGL stuff
m_IndexBuffer = indexBuffer;
}
I call the method somewhere in my code as follow:
// …
std::shared_ptr<IIndexBuffer> m_IndexBuffer;
m_IndexBuffer.reset(IIndexBuffer::Create(indices, 3)); // create the buffer…
//…
m_VertexArray->SetIndexBuffer(m_IndexBuffer); // call the OpenGLVertexArray method and pass buffer as argument.
So far so good… But then when I compile, a got the following error:
build] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(1144,9): error: no matching conversion for functional-style cast from 'const shared_ptr<Spark::IIndexBuffer>' to 'std::shared_ptr<Spark::IVertexBuffer>'
[build] shared_ptr(_Right).swap(*this);
[build] ^~~~~~~~~~~~~~~~~
[build] ..\src\modules\sparkengine\Source\Private\Platform\OpenGL\OpenGLVertexArray.cpp(75,19): note: in instantiation of function template specialization 'std::shared_ptr<Spark::IVertexBuffer>::operator=<Spark::IIndexBuffer>' requested here
[build] m_IndexBuffer = indexBuffer;
The compiler is basically complaining when I try to assign m_IndexBuffer with indexBuffer in SetIndexBuffer method.
I tried a lot of things, even passing indexBuffer as copy to the method.
Any ideas?