简体   繁体   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)

The picture not showing and when I close, error appear.图片没有显示,当我关闭时,出现错误。 I can't figure it out, maybe a problem with pointers.我想不通,可能是指针有问题。

stackoverflow is asking for details, but I have no more details - sorry. stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow is asking for details, but I have no more details - sorry. stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow is asking for details, but I have no more details - sorry. stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow is asking for details, but I have no more details - sorry. stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。 stackoverflow is asking for details, but I have no more details - sorry. stackoverflow 正在询问详细信息,但我没有更多详细信息 - 抱歉。

Menu.h菜单.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 <--------- the problem is here 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

Instead of代替

picture* menuPicture[];

use

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

And in menu constructor instead of doing this:而在menu构造函数中,而不是这样做:

this->menuPicture[this->menuPictCount];

do this:做这个:

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

Edit: Of course remove also delete s from destructor.编辑:当然 remove 也从析构函数中delete std::vector and std::unique_ptr will clean up after themselves. std::vectorstd::unique_ptr会自行清理。

If number of pictures in menu is constant (like in your constructor) you can change menuPicture declaration to:如果菜单中的图片数量是恒定的(就像在您的构造函数中一样),您可以将menuPicture声明更改为:

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

but then you can't call resize on it.但是你不能在它上面调用resize。

暂无
暂无

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

相关问题 调试断言失败! 表达式:is_block_type_valid(header-&gt; _ block_use)。 对象不会初始化和推送错误 - Debug Assertion Failed! Expression: is_block_type_valid(header->_block_use). Object wont init and Push error Debug Assertion失败OpenCv is_block_type_valid(header - > _ block_use) - Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use) 为什么我解决了“调试断言失败的OpenCv is_block_type_valid(header-&gt; _ block_use)” - WHY I solved “Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)” 为什么我会得到 _CrtIsValidHeapPointer(block) 和/或 is_block_type_valid(header-&gt;_block_use) 断言? - Why do I get _CrtIsValidHeapPointer(block) and/or is_block_type_valid(header->_block_use) assertions? 调试断言失败:_BLOCK_TYPE_IS_VALID - Debug assertion failed: _BLOCK_TYPE_IS_VALID 错误 调试断言失败。 BLOCK_TYPE_IS_VALID - Error Debug Assertion Failed. BLOCK_TYPE_IS_VALID 使用删除时引发错误调试声明失败_BLOCK_TYPE_IS_VALID - Error raised Debug Assertion Failed _BLOCK_TYPE_IS_VALID when delete is used 调试声明失败(_BLOCK_TYPE_IS_VALID)…此解决方案有效吗? - Debug Assertion Failed (_BLOCK_TYPE_IS_VALID)… Is this solution valid? 调试断言失败! 表达式:_BLOCK_TYPE_IS_VALID(pHead-&gt; nBlockUse)在程序结束 - Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) In program ended C ++调试断言失败,表达式:_BLOCK_TYPE_IS_VALID(pHead-blockUse) - C++ Debug Assertion Failed, Expression: _BLOCK_TYPE_IS_VALID(pHead - blockUse )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM