C++ 对象类型错误
基本上由于某种原因,新对象的类型是错误的。所有源代码都在github上。如果有帮助,请随意用叉子叉 我有以下课程:C++ 对象类型错误,c++,types,sfml,C++,Types,Sfml,基本上由于某种原因,新对象的类型是错误的。所有源代码都在github上。如果有帮助,请随意用叉子叉 我有以下课程: #ifndef _VIEW_HPP #define _VIEW_HPP #include <iostream> #include "sfml_drawsurface.hpp" class View { protected: View(Drawsurface& d) : drawer(d) { std::clog << "
#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\u视图\u水电站
#定义\u视图\u水电站
#包括
#包括“sfml_Drawsure.hpp”
类视图{
受保护的:
视图(绘图面和d):抽屉(d){
std::clog看起来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
变量上也有同样的问题
解决方案是不将它们作为局部变量,而是作为类成员(首选)或动态分配(这取决于您需要让它们存在多长时间)。您的include-guard都是。我在这段代码中看到的唯一奇怪之处是,没有一个派生类析构函数被声明为虚拟的。我无法说明您为什么这样做(而且它太早了,咖啡因也不够,我记不清它是否重要)。最有可能的是,drawer
在你调用draw
之前超出了范围,留下了一个对被破坏对象的悬空引用gameview
。你需要确保它和任何使用它的东西一样长。是的解决方案是将gamecontroller、game和gameview移出构造函数。现在我在创建新的pro时传递它们克对象。
/**
* 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
#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;
Program::Program() {
Game game;
...
this->gamecontroller = new Gamecontroller(game); //Probably also bad
sfml_drawsurface drawer(window);
this->gameview = new Gameview(drawer);
}