我一直在尝试修复此访问冲突异常数小时!任何帮助表示赞赏!
我试过让这个类中的所有变量都是静态的,仍然会得到未处理的异常!
Case.hpp
class CCase {
public:
Texture tex_Case;
Sprite spt_Case;
Text txt_CaseNumber;
int i_CaseNum, i_CaseValue;
bool b_PersonalCase = false, b_CaseRemoved = false;
void DrawCase(RenderWindow* window) {
FloatRect caseRect = spt_Case.getGlobalBounds();
FloatRect textRect = txt_CaseNumber.getLocalBounds();
txt_CaseNumber.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
txt_CaseNumber.setPosition(Vector2f(caseRect.left + caseRect.width / 2.0f, caseRect.top + caseRect.height / 2.0f));
window->draw(spt_Case);
window->draw(txt_CaseNumber);
}
CCase(RenderWindow* window, int CaseNum, int CaseValue) {
i_CaseNum = CaseNum;
i_CaseValue = CaseValue;
tex_Case = ENG::TextureFromFile(window, "Data//Images//Case.jpg", "DrawGame", true, false, true);
spt_Case.setTexture(tex_Case);
spt_Case.setScale(Vector2f(0.05, 0.05));
txt_CaseNumber.setFont(Vars::Draw::txtFont);
txt_CaseNumber.setString(to_string(i_CaseValue));
txt_CaseNumber.setCharacterSize(44); // in pixels
txt_CaseNumber.setFillColor(Color::White);
txt_CaseNumber.setOutlineColor(Color::Black);
txt_CaseNumber.setOutlineThickness(1);
txt_CaseNumber.setStyle(Text::Bold);
}
};
我也试过让它变成非静态的
Entry.h
static CCase* Case[26];
Entry.cpp
for (int i = 0; i < 5; i++) {
Case[i] = new CCase(window, ++i, 1);
Case[i]->spt_Case.move(Vector2f(300 + i*100, 100 + i*100));
}
for (int i = 0; i < 5; i++) {
if (!Case[i]->b_CaseRemoved) { Case[i]->DrawCase(window); /* <-- Exception Is Caused By This Func */ }
// if (ENG::ObjectIsClicked(window, Case[i]->spt_Case)) { Case[i]->b_CaseRemoved = true; }
}
另外,有没有办法同时构建数组的所有元素以节省加载时间?
答案 0 :(得分:6)
在此代码中,您将每个奇数元素保留为未分配状态:
for (int i = 0; i < 5; i++) {
Case[i] = new CCase(window, ++i, 1);
i
由于i++
和++i
两行中的双倍增量而迭代0,2,4。