Advertisement

Assign shared pointer member

Started by February 23, 2020 07:54 PM
2 comments, last by Tiago Patrocinio 4 years, 9 months ago

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>&amp; indexBuffer) override; // method declaration

	//…
};

// Source
void OpenGLVertexArray::SetIndexBuffer(const std::shared_ptr<IIndexBuffer>&amp; 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?

The error message is clear enough. You are trying to assign a std::shared_ptr<IIndexBuffer> to a variable of type std::shared_ptr<IVertexBuffer>, and IIndexBuffer and IVertexBuffer are not compatible types.

Advertisement

Oh…! I dont believe that I made this mistake… I was reading the compiler log so fast and thought that was IIndexBuffer. hahaha That's it. I was stuck on this for 1 hour. Thank you!

This topic is closed to new replies.

Advertisement