我不明白為什么,但是在我將類指標放入陣列后,std::vector 沒有給出任何東西。
// runs at start
void States::AssignState(GameState* state) {
_nextVacentState ;
_states.push_back(state);
}
// executes in a loop
void States::ExecuteCurrentState() {
// protection incase there is nothing in the array or the current state is not grater than the size of the array (not the problem after i nerrowed the problem down)
if (_nextVacentState == 0) std::cout << "Error: There is no states, setup some states then try again" << std::endl; return; // there is no states
if (_currentState >= _states.size() - 1) std::cout << "Error: Current State is grater than all possable states" << std::endl; return;
// The program just freezes at this and i can figure out why
_states[0]->tick();
std::printf("S");
}
uj5u.com熱心網友回復:
這是我建議養成對所有陳述句使用大括號的習慣的原因之一if,即使是單行的陳述句。
問題線:
if (_nextVacentState == 0) std::cout << "Error: There is no states, setup some states then try again" << std::endl; return;
讓我們添加一些換行符以更清楚地了解正在發生的事情
if (_nextVacentState == 0)
std::cout << "Error: There is no states, setup some states then try again" << std::endl;
return;
該return陳述句將無條件執行,因為只有后面的第一條陳述句if(_nextVacentState==0)實際上是if. 所以編譯器執行它就好像它是這樣寫的:
if (_nextVacentState == 0)
{
std::cout << "Error: There is no states, setup some states then try again" << std::endl;
}
return;
但是,你想要的東西需要這樣寫:
if (_nextVacentState == 0)
{
std::cout << "Error: There is no states, setup some states then try again" << std::endl;
return;
}
在下一次if檢查中您也有同樣的問題_currentState。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479305.html
上一篇:如何通過menuitem呼叫另一個活動?AndroidStudio錯誤:預期方法呼叫
下一篇:為類的每個實體定義不同的函式
