转换c ++无效

时间:2014-04-17 14:37:23

标签: c++ sdl

我收到此错误

  

来自' GameObject *'的无效转换to' std :: vector :: value_type {aka SDLGameObject *}' [-fpermissive]

这是我的代码我不知道出了什么问题,因为在我的班级MainMenu中,这是我的班级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在这里工作!!

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;
}

1 个答案:

答案 0 :(得分:1)

SDLGameObject派生自基类GameObject。您的向量m_gameObjects存储了指向SDLGameObject的指针,但您指的是指向GameObject的指针。

无法进行此转换,因为GameObject不一定是SDLGameObject

如果你确定是这种情况,你可以这样做:

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

或更改m_gameObjects的定义以存储指向GameObject的链接。