【问题标题】:C++ Access Violation Class ArrayC++ 访问冲突类数组
【发布时间】:2018-11-16 20:39:20
【问题描述】:

几个小时以来,我一直在尝试修复此访问冲突异常!任何帮助表示赞赏!

我也尝试将这个类中的所有变量都设为静态,但仍然得到未处理的异常!

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; }
}

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

【问题讨论】:

    标签: c++ class exception sfml


    【解决方案1】:

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

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

    由于i++++i 在两行中的双重递增,i 迭代了 0、2、4。

    【讨论】:

    • 谢谢你!我忘记了 ++i 迭代 i 而不是仅仅添加 i + 1。有趣的是,最简单的错误是我们停留时间最长的错误。 (至少对我来说):)
    猜你喜欢
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多