1

создал 2 класса:

1) Window.hpp

#ifndef WINDOW_HPP
#define WINDOW_HPP

#include <SFML/Graphics.hpp>
#include "Widgets/Button.hpp"
#include <iostream>
using namespace std;

class Window{
public:
    Window(){
        window.create(sf::VideoMode(width, height), "Window", sf::Style::Close);
        window.setPosition(sf::Vector2i(desktop_width/2-width/2, desktop_height/2-height/2));

        Button_void();

        while(window.isOpen()){
            while(window.pollEvent(event)){
                if(event.type==sf::Event::Closed)
                    window.close();
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                    window.close();
            }

            window.clear();
            window.display();
        }
    }

    void Button_void(){
        button.setSize(200, 200);
        button.setBackgroundColor(255, 231, 0);
        button.setPosition(0, 0);
    }
private:
    sf::Button button;

    const int width=800;
    const int height=800;

    const int desktop_width=sf::VideoMode::getDesktopMode().width;
    const int desktop_height=sf::VideoMode::getDesktopMode().height;

    sf::RenderWindow window;
    sf::Event event;
};

#endif // WINDOW_HPP

2) Button.hpp

#ifndef BUTTON_HPP
#define BUTTON_HPP

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

namespace sf{
    class Button : public sf::RectangleShape{
    public:
        void setSize(float width, float height){
            Button::setSize(width, height);
        }
        void setPosition(float x, float y){
            Button::setPosition(x, y);
        }
        void setBackgroundColor(int r, int g, int b){
            Button::setFillColor(sf::Color(r, g, b));
        }
    };
}

#endif // BUTTON_HPP

Код компиляции:

COMPILER = g++
FILENAME = Program.cpp
OUTPUT = Program
PARAMS = -lsfml-graphics -lsfml-system -lsfml-window

output:
    ${COMPILER} ${FILENAME} -o ${OUTPUT} ${PARAMS}

Ощибка: Segmentation Fault

Помогите пж :)

1 ответ 1

5

Не вижу где у вас SegFault получается, но вот такие вызовы приводят к бесконечному самовызову (рекурсии) функций:

class Button : public sf::RectangleShape{
    public:
        void setSize(float width, float height){
            Button::setSize(width, height);

Видимо надо так:

class Button : public sf::RectangleShape{
    public:
        void setSize(float width, float height){
            sf::RectangleShape::setSize(width, height);

Остальные функции необходимо исправить аналогично

1
  • 2
    Спасибо, всё отлично работает!
    – Red Bull
    6 апр 2020 в 17:02

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.