Int赋值在构造函数中混淆ifstream

时间:2017-01-20 09:59:22

标签: c++ class constructor int ifstream

这真的很奇怪,或者我是超级盲目和/或愚蠢的。

这个想法是在构造函数中打开一个文件,加载它的内容,关闭它然后继续。它工作得很好,直到我添加了另一个私有变量来保持玩家的积分。

class WordGame
{
    private:
        bool _hasLost;
        std::vector<Set> vecSets; //Set is another class :)
        int nPoints;

    public: 
        bool hasLost();
        WordGame(std::string);
        void showSet(Set);
        void nextSet();
};

现在对于奇怪的东西,这是我的构造函数:

WordGame::WordGame(std::string strFileName)
{
    this->_hasLost = false;

    /* Here the magic happens:
       nPoints = 0 -> "File does not exist"
       nPoints = other than 0 -> crash
       comment out -> works fine */

    //this->nPoints = 1;

    std::ifstream file(strFileName);
    if (!file.good()) {
        std::cout << "File does not exist!";
        exit(1);
    }

    while (!file.eof()) {
        std::string strSentence, strHint;

        getline(file, strSentence);
        getline(file, strHint);

        Set set(strSentence, strHint);

        this->vecSets.push_back(set);
    }

    file.close();
}

主要功能:

int main(int argc, char **argv)
{
    srand(time(0));
    WordGame game("test.txt");


    while (!game.hasLost()) {
        game.nextSet();
    }
}

这里有什么问题?我没有想法。

0 个答案:

没有答案