Question

I get this error

invalid conversion from 'GameObject*' to 'std::vector::value_type {aka SDLGameObject*}' [-fpermissive]

This is my code I don't know what is wrong because in my class MainMenu it works this is my class Playstate.cpp

bool PlayState::onEnter()
{
    SDL_ShowCursor(1);
        //parse the state
        TheTextureManager::Instance()->load("assets/button.png", "test", TheGame::Instance()->getRenderer());
        GameObject* pGameObject1 = TheGameObjectFactory::Instance()->create("MenuButton");
        pGameObject1->load(new LoaderParams(120, 300, 400, 100, "test", 4, 4, 4));

        m_gameObjects.push_back(pGameObject1);
}

GameObject.h

#include "LoaderParams.h"
#include <string>
using namespace std;


class GameObject {
public:

    virtual void draw() = 0;
    virtual void update() = 0;
    virtual void clean() = 0;

     virtual void load(const LoaderParams* pParams)=0;

protected:

     GameObject() {}
     virtual ~GameObject() {}
};

#endif /* GAMEOBJECT_H_ */

SDLGameObject.h

#ifndef SDLGAMEOBJECT_H_
#define SDLGAMEOBJECT_H_
#pragma once
#include "GameObject.h"
#include "LoaderParams.h"
#include "TextureManager.h"
#include "Vector2D.h"


class SDLGameObject : public GameObject {
public:
     SDLGameObject();
     virtual void draw();
     virtual void update();
     virtual void clean(){}
     virtual void load(const LoaderParams* pParams);

     Vector2D& getPosition() { return m_position; }
     virtual int getWidth() { return m_width; }
     virtual int getHeight() { return m_height; }



protected:
     Vector2D m_position;
     Vector2D m_velocity;
     Vector2D m_acceleration;

     int m_width;
     int m_height;

     int m_currentRow;
     int m_currentFrame;

     std::string m_textureID;

};

#endif /* SDLGAMEOBJECT_H_ */

MainMenuState.cpp here it does work!!

bool MainMenuState::onEnter()
{
    SDL_ShowCursor(1);
    //parse the state
    TheTextureManager::Instance()->load("assets/button.png", "playbutton" , TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("assets/exit.png", "exitbutton" , TheGame::Instance()->getRenderer());
    GameObject* pGameObject = TheGameObjectFactory::Instance()->create("MenuButton");
    GameObject* pGameObject1 = TheGameObjectFactory::Instance()->create("MenuButton");
    pGameObject->load(new LoaderParams(120, 150, 400, 100, "playbutton", 0, 1, 2));
    pGameObject1->load(new LoaderParams(120, 300, 400, 100, "exitbutton", 1, 2, 2));

    m_gameObjects.push_back(pGameObject);
    m_gameObjects.push_back(pGameObject1);
    m_callbacks.push_back(0);
    m_callbacks.push_back(s_menuToPlay);
    m_callbacks.push_back(s_exitFromMenu);

    //set callbacks for menu items
    setCallbacks(m_callbacks);
    std::cout << "Entering MainMenuState\n";
    return true;
}
Was it helpful?

Solution

SDLGameObject derives from the base class GameObject. Your vector m_gameObjects stores pointers to SDLGameObject, but you are giving it a pointer to a GameObject.

This conversion is not possible as a GameObject is not necessarily an SDLGameObject.

If you are sure that this is the case you can do this:

SDLGameObject* p = dynamic_cast<SDLGameObject>(pGameObject1);
if(!p) {
    // pGameObject1 is actually not an SDLGameObject
}
m_gameObjects.push_back(p);

Or change the definition of m_gameObjects to store pointers to GameObject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top