简体   繁体   中英

C++ Access Violation Class Array

I have been trying to fix this Access Violation exception for hours! Any help is appreciated!

I have tried making all variables in this class static as well, still get the unhandled exception!

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

I have also tried making it non-static

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

Also, is there a way to constuct all elements of the array all at once to save loading time?

In this code you left each odd element unassigned:

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

i iterates through 0, 2, 4 due to double increment in i++ and ++i in two lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM