对象类型错误

时间:2014-10-25 14:51:18

标签: c++ types sfml

基本上由于某种原因,新对象是错误的类型。所有源代码都在github https://github.com/teuro/sfml-radar上。如果有帮助请随意分叉。

我有以下课程:

#ifndef _VIEW_HPP
#define _VIEW_HPP

#include <iostream>

#include "sfml_drawsurface.hpp"

class View {
protected:
    View(Drawsurface& d) : drawer(d) {
        std::clog << "View::View()" << std::endl;
    }

    Drawsurface& drawer;

    virtual void draw() = 0;
};


#endif

这是所有不同类型视图的基类。现在我已经派生了子类

#ifndef _GAME_VIEW_HPP
#define _GAME_VIEW_HPP

#include <vector>
#include <iostream>
#include <typeinfo>

#include "view.hpp"
#include "../models/game.hpp"

class Gameview : public View {
public:
    Gameview(Drawsurface& d);
    ~Gameview();
    void draw();
private:
    Drawsurface& drawer;
};

#endif // _GAME_VIEW_HPP

然后抽象类Drawsurface

/**
    * drawsurface base for all graphics pure abstract
    * provide only interface quite high-level
    * 2014/06/02
    * Juha Teurokoski
**/

#ifndef _DRAWSURFACE_HPP
#define _DRAWSURFACE_HPP

#include <string>

#include "../models/point.hpp"

class Drawsurface {
public:
    bool font_loaded;
    virtual void rectangleColor(Point& a, Point& b, unsigned int color) = 0;
    virtual void lineColor(Point& a, Point& b, unsigned int color) = 0;
    virtual void circleColor(Point& a, unsigned int rad, unsigned int color) = 0;
    virtual void trigonColor(Point& a, Point& b, Point& c, unsigned int color) = 0;
    virtual void trigonColor(Point& a, unsigned int size, unsigned int color) = 0;
    virtual void load_font(std::string font) = 0;

    virtual void draw_picture(std::string tiedosto, Point& a, bool center = false) = 0;
    virtual void draw_text(std::string text, Point& a, unsigned int color = 0) = 0;

    virtual int get_fontsize() = 0;
    virtual void flip() = 0;
    virtual void clear_screen() = 0;

    virtual ~Drawsurface() { }
};

#endif

现在,如果我创建了一个新的sfml_drawsurface实例,它是Drawsurface的子类。由于某种原因,新对象是Drawsuface而不是sfml_drawsurface。下面是sfml_drawsurface类。

#ifndef SFML_DRAWSURFACE_HPP
#define SFML_DRAWSURFACE_HPP
/**
    * sfml-drawsurface provides basic drawing, pictures and text
    * require drawsurface
    * 2014/06/02
    * Juha Teurokoski
**/

#include "drawsurface.hpp"

#include <vector>
#include <stdexcept>
#include <iostream>

#include <SFML/Graphics.hpp>

class sfml_drawsurface : public Drawsurface {
public:
    sfml_drawsurface(sf::RenderWindow& window);
    ~sfml_drawsurface();

    void rectangleColor(Point& a, Point& b, unsigned int color);
    void circleColor(Point& a, unsigned int rad, unsigned int color);
    void lineColor(Point& a, Point& b, unsigned int color);
    void trigonColor(Point& a, Point& b, Point& c, unsigned int color);
    void trigonColor(Point& a, unsigned int _size, unsigned int color);

    void draw_picture(std::string tiedosto, Point& a, bool center = false);
    void draw_text(std::string text, Point& a, unsigned int color);
    void load_font(std::string font);
    void clear_screen();

    int get_fontsize();
    void flip();
protected:
private:
    sf::RenderWindow& window;
    sf::Font font;

    sf::Color active;
    sf::Color normal;
};

#endif // SFML_DRAWSURFACE_HPP

我创建了这样的新对象:

sfml_drawsurface drawer(window);

this->gameview = new Gameview(drawer);
std::clog << typeid(drawer).name() << std::endl;

一切似乎都是正确的,因为std :: clog outout是&#39; 16sfml_drawsurface&#39;。

接下来的地方是draw-method然后发生了一些非常奇怪的事情。

同样的印刷品现在是&#39; 11Drawsurface&#39;。

1 个答案:

答案 0 :(得分:1)

看起来迈克有正确的想法。从您在构造函数中的Program.cpp文件中:

Program::Program() {
    Game game;
    ...
    this->gamecontroller = new Gamecontroller(game);  //Probably also bad

    sfml_drawsurface drawer(window);
    this->gameview = new Gameview(drawer);  
}

问题是,一旦构造函数完成后,drawer就不再存在,只留下悬空引用和未定义的行为。您可能对game变量有同样的问题。

解决方案是不将它们作为局部变量,而是作为类成员(首选)或动态分配(它取决于你需要多长时间)。