分段错误(可以用初始化列表解决,不能用复制构造函数解决)

segmentation fault (can solve with initialization list, can't with copy constructor)

提问人:DR4NKR1D3R 提问时间:10/30/2023 最后编辑:DR4NKR1D3R 更新时间:10/30/2023 访问量:89

问:

我的代码中出现分段错误:

#include <SFML/Graphics.hpp>

#include "game.hpp"
#include "map.hpp"
#include "button.hpp"


class Game {
private:
   Map map;
   Button play_button;
   Button menu_button;
   std::string state = "menu";
public:
    Game(sf::RenderWindow &window) {
        map.setBlockWidth(window);

        sf::Vector2u window_size = window.getSize();
        int button_width = window_size.x / 4;
        int button_height = window_size.y / 10;

        play_button = Button(sf::Vector2f(button_width, button_height), sf::Vector2f((window_size.x / 2) - (button_width / 2), (window_size.y / 2) - (button_height / 2)), [this]() {state = "game";}, "PLAY");
        menu_button = Button(sf::Vector2f(window_size.x / 10, window_size.y / 20), sf::Vector2f(10, 10), [this]() {state = "menu";}, "MENU");
    }

    ~Game() {}
    
    void update(sf::RenderWindow &window, bool LMB_pressed) {
        if (state == "menu") {
            if (LMB_pressed) {
                play_button.detectOnClick(window);
            }

            renderMenu(window);
        } else if (state == "game") {
            if (LMB_pressed) {
                menu_button.detectOnClick(window);
            }

            map.update(window);
            renderInterface(window);
        }
    }

    void renderMenu(sf::RenderWindow &window) {
        window.draw(play_button);
    }

    void renderInterface(sf::RenderWindow &window) {
        window.draw(menu_button);
    }
};

我可以通过将下面的代码从构造函数移动到更新方法来解决这个问题,但这是一个可怕的解决方案。

sf::Vector2u window_size = window.getSize();
        int button_width = window_size.x / 4;
        int button_height = window_size.y / 10;

        play_button = Button(sf::Vector2f(button_width, button_height), sf::Vector2f((window_size.x / 2) - (button_width / 2), (window_size.y / 2) - (button_height / 2)), [this]() {state = "game";}, "PLAY");
        menu_button = Button(sf::Vector2f(window_size.x / 10, window_size.y / 20), sf::Vector2f(10, 10), [this]() {state = "menu";}, "MENU");

另一种解决方案是使用初始化列表:

Game(sf::RenderWindow &window) : play_button(sf::Vector2f(0, 0), sf::Vector2f(0, 0), [this]() {state = "game";}, "PLAY"),
                                     menu_button(sf::Vector2f(0, 0), sf::Vector2f(0, 0), [this]() {state = "menu";}, "MENU") {
        map.setBlockWidth(window);

        sf::Vector2u window_size = window.getSize();
        int button_width = window_size.x / 4;
        int button_height = window_size.y / 10;

        play_button.setPosition(sf::Vector2f((window_size.x / 2) - (button_width / 2), (window_size.y / 2) - (button_height / 2)));
        play_button.setSize(sf::Vector2f(button_width, button_height));

        menu_button.setPosition(sf::Vector2f(10, 10));
        menu_button.setSize(sf::Vector2f(window_size.x / 10, window_size.y / 20));
    }

问题是我需要编写额外的 setXXX 方法

理想情况下,我想使用复制构造函数(我认为它可能有效并且是最聪明的想法),但我无法使它与编译器自动生成的构造函数一起使用。

问题似乎在于将 Button 对象创建为成员变量,然后将在构造函数中创建的按钮对象分配给该成员变量。看起来离开后,构造函数对象被删除了。

C++ 复制构造函数 初始值设定项列表

评论


答: 暂无答案