gpt4 book ai didi

c++ - 访问冲突运行时错误。为什么会发生?

转载 作者:行者123 更新时间:2023-11-30 05:27:41 28 4
gpt4 key购买 nike

我的问题是,当我想将代码分成更小的函数时,它会抛出一个运行时错误:访问冲突。

Error dialog image

因为一切都是按值传递的,所以我不知道为什么会发生这样的错误。与内存无关。

通用代码:

                                // 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) 线]

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

最佳答案

错误(如果我没记错的话)很简单

在这个构造函数中

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.:抱歉我的英语不好。

关于c++ - 访问冲突运行时错误。为什么会发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com