对象的类型错误

Object is wrong type

本文关键字:错误 类型 对象      更新时间:2023-10-16

基本上由于某种原因,新对象的类型是错误的。所有源代码都在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::clock-outout是"16sfml_drawsurface"。

接下来是绘制方法,然后发生了一些非常奇怪的事情。

同样的印刷品现在是"11Drawsurface"。

看起来Mike的想法是对的。从您的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变量有同样的问题。

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