【问题标题】:the picture is not displayed and an error occurs when turning off --> Debug Assertion Failed! Expression: is_block_type_valid(header->_block_use)图片不显示,关闭时出错 --> Debug Assertion Failed!表达式:is_block_type_valid(header->_block_use)
【发布时间】:2020-01-04 23:14:53
【问题描述】:

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

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

Menu.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

【问题讨论】:

  • this-&gt;menuPicture[this-&gt;menuPictCount]; 不分配任何空间。它根本没有做任何事情。
  • 但是如果我只需要在构造函数实现中初始化数组呢?
  • 你没有数组,你有一个指向图片的指针。您需要为其分配空间(使用new)。更好的是,摆脱原始指针并改用智能指针和标准容器。
  • 注意:delete[] menuPicture; 只删除了指针数组。它没有delete 动态分配并放置在数组中的对象。重要提示:这里不需要动态分配任何东西。你知道menuPicture 的大小是4,你可以使用pictures 或std::array 的普通旧数组。蓝精灵这个。我要写一个非答案。
  • 没关系 看起来 chwala 正在报道这个。

标签: c++ visual-studio pointers debugging expression


【解决方案1】:

代替

picture* menuPicture[];

使用

std::vector&lt;std::unique_ptr&lt;picture&gt;&gt; menuPicture;

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

this-&gt;menuPicture[this-&gt;menuPictCount];

这样做:

this-&gt;menuPicture.resize(this-&gt;menuPictCount);

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

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

std::array&lt;std::unique_ptr&lt;picture&gt;, 4&gt; menuPicture;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2017-01-25
    • 2021-02-01
    • 2012-03-28
    • 1970-01-01
    • 2021-05-04
    • 2022-01-23
    相关资源
    最近更新 更多