C++ 访问冲突类数组

标签 c++ class exception sfml

几个小时以来,我一直在尝试修复此访问冲突异常!感谢您的帮助!

我也试过将这个类中的所有变量设为静态,但仍然出现未处理的异常!

案例.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];

入门.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; }
}

另外,有没有办法一次构造数组的所有元素以节省加载时间?

最佳答案

在此代码中,您未分配每个奇数元素:

for (int i = 0; i < 5; i++) {
     Case[i] = new CCase(window, ++i, 1);

i 由于 i++++i 在两行中进行了两次递增,因此迭代了 0、2、4。

关于C++ 访问冲突类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50732618/

相关文章:

C++:使用未声明的标识符 'u8'

c++ - 为什么我在 MinGW 中不需要标志 -lm 但在 Linux 中我明确需要它?

python - 在 Pyside 插槽中意外处理异常

Python - 如何在方法内部捕获外部异常

c++ - 如何将类作为参数传递

python - 程序要引发的标准 python 异常列表在哪里?

c++ - 模板特化和普通的旧功能

python - 为 QUuid 类制作 GDB 调试助手

java - Wsimport 生成的类和我的原始 Web 服务类中的名称冲突

c++ - 在成员构造函数之后调用基类构造函数