![](/img/trans.png)
[英]Debug Assertion Failed! Expression: is_block_type_valid(header->_block_use). Object wont init and Push error
[英]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::vector
和std::unique_ptr
會自行清理。
如果菜單中的圖片數量是恆定的(就像在您的構造函數中一樣),您可以將menuPicture
聲明更改為:
std::array<std::unique_ptr<picture>, 4> menuPicture;
但是你不能在它上面調用resize。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.