为什么我的 C++ 应用程序以退出值 -1 终止,但某些代码逻辑未被执行?

Why is my C++ app terminated with exit value -1 but some code logic isn't being executed?

基本上我有这个主要方法

int main() {
GameMapDriver* gameMapDriver = new GameMapDriver();
int players = 0;
std::cout << "How many players are going to play the game? Insert a numeric value from 2 to 5  : ";
std::cin >> players;

if ( players == 2 )
    gameMapDriver->createGameMapForNumberOfPlayers(2);
switch (players)  {
case 2 : {
    gameMapDriver->createGameMapForNumberOfPlayers(2);
    break;
}
case 3: {
    gameMapDriver->createGameMapForNumberOfPlayers(3);
    break;
}
case 4: {
    gameMapDriver->createGameMapForNumberOfPlayers(4);
    break;
}
case 5: {
    gameMapDriver->createGameMapForNumberOfPlayers(5);
    break;
}
default: std::cout << "Invalid number of players";
break;
}
return 0;
}

对于 gameMapDriver->createGameMapForNumberOfPlayers(2) 方法(请注意第一个 if 语句用于调试目的,它不应该影响任何东西),我有这个(我裁剪了图片,开关被编码正确地在末尾使用默认大小写):

GameMap * GameMapDriver::createGameMapForNumberOfPlayers(int players){
std::cout << "Creating map";
    switch (players){
    case (TWO):{
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(TWO);
        std::cout << "Game map for two players created";
        return this->gameMap;
        break;
    }
    case (THREE) : {
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(THREE);
        std::cout << "Game map for three players created";
        return this->gameMap;
        break;
    }
    case (FOUR): {
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(FOUR);
        std::cout << "Game map for four players created";
        return this->gameMap;
        break;
    }
    case (FIVE):{
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(FIVE);
        std::cout << "Game map for five players created";
        return this->gameMap;
        break;
    }
    default: {
        std::cout << "Invalid number of players";

        return this->gameMap;
        break;
    }
    }
    }

问题是来自 createGameMapForNumberOfPlayers 的第 21 行 std::cout << "Creating map"; 它甚至不在控制台上打印。

我 运行 我的应用程序,我输入“2”,我读了它,我应该将它传递给我的方法,但它只是说(退出值 -1)。它甚至不打印 "Creating Map" 完全没有。

发生了什么事? :(

你应该使用调试器,但如果你不能,那么至少你应该刷新 cout 以更好地了解你的程序哪里出错了。

std::cout << "Creating map" << std::endl;

std::endl 将向输出流添加一个换行符,更重要的是强制输出立即发生。我猜你的程序正在崩溃,而输出仍在等待发生。