我的问题是,当我想将代码分成更小的函数时,它会抛出一个运行时错误:访问冲突。
由于所有内容都是按值传递的,所以我不知道为什么会出现这样的错误。关于记忆无关。
一般代码:
// main.cpp
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "GameEngine.h"
int main()
{
sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(200, 200), "SFML works!");
GameEngine game(window);
game.run();
delete window;
return 0;
}
// DrawableVertices.h
#pragma once
#include "SFML\Graphics.hpp"
using namespace sf;
class DrawableVertices : public Drawable, public Transformable
{
VertexArray vertices;
Texture* pTexture;
public:
virtual void draw(RenderTarget& target, RenderStates states) const;
DrawableVertices(VertexArray vertices, Texture* pTexture = nullptr);
DrawableVertices();
Texture* getTexture();
VertexArray* getVertices();
};
// DrawableVertices.cpp
#include "stdafx.h"
#include "DrawableVertices.h"
DrawableVertices::DrawableVertices(sf::VertexArray _vertices, sf::Texture* _pTexture)
{
vertices = _vertices;
pTexture = pTexture;
}
DrawableVertices::DrawableVertices()
{
vertices = sf::VertexArray(sf::Points, 1);
pTexture = nullptr;
}
void DrawableVertices::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = pTexture;
target.draw(vertices, states);
}
sf::Texture* DrawableVertices::getTexture() { return pTexture; }
sf::VertexArray* DrawableVertices::getVertices() { return &vertices; }
工作代码(全部在一个函数中):
//GameEngine.h
#pragma once
#include "DrawableVertices.h"
class GameEngine
{
RenderWindow* pWindow;
public:
GameEngine(RenderWindow* window);
void run();
};
//GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"
GameEngine::GameEngine(RenderWindow* window)
{
pWindow = window;
}
void GameEngine::run()
{
const int X_DIFF = 8;
const int PLAYER_Y = 24;
const int PLAYER_X = 48;
VertexArray playerShape(TrianglesStrip, 4);
playerShape[0].position = Vector2f(X_DIFF, 0);
playerShape[0].color = Color(0, 127, 255);
playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
playerShape[1].color = Color(0, 127, 255);
playerShape[2].position = Vector2f(0, PLAYER_Y);
playerShape[2].color = Color(0, 0, 255);
playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
playerShape[3].color = Color(0, 0, 255);
DrawableVertices player(playerShape);
player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
player.setPosition(64, 64);
while (pWindow->isOpen())
{
Event event;
while (pWindow->pollEvent(event))
{
if (event.type == Event::Closed)
pWindow->close();
}
pWindow->clear();
pWindow->draw(player);
pWindow->display();
}
}
不工作代码,在绘制播放器时抛出访问冲突 [pWindow-> draw(player)line]
//GameEngine.h
#pragma once
#include "DrawableVertices.h"
class GameEngine
{
RenderWindow* pWindow;
DrawableVertices player;
void createPlayer();
public:
GameEngine(RenderWindow* window);
void run();
};
// GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"
GameEngine::GameEngine(RenderWindow* window)
{
pWindow = window;
createPlayer();
}
void GameEngine::createPlayer()
{
const int X_DIFF = 8;
const int PLAYER_Y = 24;
const int PLAYER_X = 48;
VertexArray playerShape(TrianglesStrip, 4);
playerShape[0].position = Vector2f(X_DIFF, 0);
playerShape[0].color = Color(0, 127, 255);
playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
playerShape[1].color = Color(0, 127, 255);
playerShape[2].position = Vector2f(0, PLAYER_Y);
playerShape[2].color = Color(0, 0, 255);
playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
playerShape[3].color = Color(0, 0, 255);
player = DrawableVertices(playerShape);
player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
player.setPosition(64, 64);
}
void GameEngine::run()
{
while (pWindow->isOpen())
{
Event event;
while (pWindow->pollEvent(event))
{
if (event.type == Event::Closed)
pWindow->close();
}
pWindow->clear();
pWindow->draw(player);
pWindow->display();
}
}
当我在run()方法中显示所有玩家的位置等信息时,它会显示正确的结果。这意味着玩家正确初始化并且它应该存在。
很抱歉有很多代码,但想展示一些有用的东西。显然我的整个代码更大,我只是将部分问题放到一个单独的项目中。
//编辑
是的,调试器说播放器的pTexture成员指针不好。
image from debugging working and not working code
我认为重载=运算符可能会解决问题。但是,我想了解这段代码是如何工作的,没有任何问题:
void GameEngine::run()
{
DrawableVertices player = DrawableVertices(playerShape);
// ...
pWindow->draw(player);
}
将它打包成功能并不是:
void GameEngine::createPlayer()
{
// ...
DrawableVertices player = DrawableVertices(playerShape);
}
void GameEngine::run()
{
createPlayer();
// ...
pWindow->draw(player);
}
答案 0 :(得分:1)
错误(如果我没错)是非常简单的
在此构造函数中
DrawableVertices::DrawableVertices(sf::VertexArray _vertices, sf::Texture* _pTexture)
{
vertices = _vertices;
pTexture = pTexture;
}
你自己复制pTexture
;所以pTexture
开始未定义并保持未定义。
我想你的意图是
DrawableVertices::DrawableVertices(sf::VertexArray _vertices, sf::Texture* _pTexture)
{
vertices = _vertices;
pTexture = _pTexture; // _pTexture !
}
p.s:抱歉我的英语不好。