繁体   English   中英

图片不显示,关闭时出现错误 --> Debug Assertion Failed! 表达式:is_block_type_valid(header->_block_use)

[英]the picture is not displayed and an error occurs when turning off --> Debug Assertion Failed! Expression: is_block_type_valid(header->_block_use)

图片没有显示,当我关闭时,出现错误。 我想不通,可能是指针有问题。

stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。

菜单.h

#include "Picture.h"

class menu
{
    const int menuPictCount;
    picture* menuPicture[];
public:
    menu(sf::RenderWindow& usableArea);
    ~menu();
    void draw();
    void posСorrection();
    void shineButton(sf::Color color);
};


Menu.cpp <--------- 问题出在这里


menu::menu(sf::RenderWindow& usableArea) : menuPictCount(4)
{
    this->menuPicture[this->menuPictCount];

    //background
    this->menuPicture[0] = new picture(usableArea, "image/background/0.png");

    //buttons
    this->menuPicture[1] = new picture(usableArea, "image/menu/newgame.png");
    this->menuPicture[1] = new picture(usableArea, "image/menu/continue.png");
    this->menuPicture[3] = new picture(usableArea, "image/menu/exit.png");
}
menu::~menu()
{
    for (size_t i = 0; i < this->menuPictCount; i++) delete this->menuPicture[i];
    delete[] menuPicture;
}
void menu::draw()
{
    for (size_t i = 0; i < this->menuPictCount; i++) this->menuPicture[i]->draw();
}
void menu::posСorrection()
{
    const int buttonsCount = this->menuPictCount - 1;

    if (buttonsCount % 2 == 0)
    {
        int indexCenterOver = buttonsCount / 2;
        int indexCenterUnder = indexCenterOver + 1;
        this->menuPicture[indexCenterOver]->putCenterOver();
        this->menuPicture[indexCenterUnder]->putCenterUnder();

        for (size_t i = indexCenterOver - 1; i > 0; i--)
        {
            this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
        }
        for (size_t i = indexCenterUnder + 1; i <= buttonsCount; i++)
        {
            this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
        }
    }
    else
    {
        const int indexCenter = buttonsCount / 2 + 1;

        this->menuPicture[indexCenter]->putCenter();

        for (size_t i = indexCenter - 1; i > 0; i--)
        {
            this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
        }
        for (size_t i = indexCenter + 1; i <= buttonsCount; i++)
        {
            this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
        }
    }
}
void menu::shineButton(sf::Color color)
{
    /*for (size_t i = 0; i <= this->buttons.GetCount(); i++)
    {
        if (this->buttons.GetElement(i)->data.trackContainsCursor())
            if (this->buttons.GetElement(i)->data.getColor() != color)
            {
                this->buttons.GetElement(i)->data.setColor(color);
            }
        if (!this->buttons.GetElement(i)->data.trackContainsCursor())
            if (this->buttons.GetElement(i)->data.getColor() == color)
            {
                this->buttons.GetElement(i)->data.setColor(color);
            }
    }*/
} ```


  [1]: https://i.stack.imgur.com/uNY8B.png

代替

picture* menuPicture[];

std::vector<std::unique_ptr<picture>> menuPicture;

而在menu构造函数中,而不是这样做:

this->menuPicture[this->menuPictCount];

做这个:

this->menuPicture.resize(this->menuPictCount);

编辑:当然 remove 也从析构函数中delete std::vectorstd::unique_ptr会自行清理。

如果菜单中的图片数量是恒定的(就像在您的构造函数中一样),您可以将menuPicture声明更改为:

std::array<std::unique_ptr<picture>, 4> menuPicture;

但是你不能在它上面调用resize。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM