无法将'{1、2、3、4、5、6}'转换为列表 C ++?

问题描述 投票:0回答:1

我是新手,我正在研究列表,向量等。我有一些Mini-SHRDLU的源代码,当我尝试对其进行编译时,它会显示错误消息:

C ++ 98'val'中的[[Error]必须由构造函数而不是'{...}']初始化

[[错误]无法将转换为'std :: list'

错误来自行:

list<int> val = {1,2,3,4,5,6};

Game.h代码:

#ifndef Game_h
#define Game_h

class Game{
private:
    State state;
    Board board;
    SolverHD solverHD;
public:
    Game(){};

    void getIntBoard(){
        board.generateIntlState();
        cout << "Intial State:" << endl;
        board.printBoard();
    }

    void playGame(){

        priority_queue<Goal> goals;
        getUserGoal(goals);

        solverHD.searchForGoal(board, goals);

    }


    void getUserGoal(priority_queue<Goal>& goals){
        bool added = false;
        int v, r, c;
        string enterMore;
        vector<Goal>goal_vec;
        vector<int> usedBlocks;
        bool blockUsed = false;

        list<int> val = {1,2,3,4,5,6};

        do{
            cout << "Input a goal:" << endl;
            do{
                cout << "Block (1-6):";
                cin >> v;
            } while (v < 1 || v > 6);

            do{
                cout << "Row (0-2):";
                cin >> r;
            } while (r < 0 || r > 2);

            do{
                cout << "Column (0-2):";
                cin >> c;
            } while (c < 0 || c > 2);

            for(int i = 0; i < usedBlocks.size(); i++){
                if(usedBlocks[i] == v){
                    cout << "\nA goal of this block has already been added" << endl;
                    blockUsed = true;
                    break;
                }
                else{
                    blockUsed = false;
                }
            }
            // put used blocks in vect
            usedBlocks.push_back(v);
            val.remove(v);


            // add goal to queue
            if (!blockUsed){
                Goal g;
                g.setGoal(v, r, c);
                goal_vec.push_back(g);
                goals.push(g);

                cout << "\nGoal (" << v << ", "
                << r << ", " << c << ") added\n";
            }

            cout << "\nPress y to enter another goal, otherwise press any key to quit" << endl;
            cin >> enterMore;


        }while (enterMore == "y" || enterMore == "Y");


        // USE TOWER OF HANOI CONCEPT TO REACH GOAL
        while (!added){
            int valToUse;
            for (int i = 0; i < goal_vec.size(); i++) {

                if(goal_vec[i].getRow() > 0){
                    for (int r = 0; r < goal_vec[i].getRow(); r++ ){
                        valToUse = val.front();
                        val.pop_front();

                        Goal g;
                        g.setGoal(valToUse, r, goal_vec[i].getCol());
                        goals.push(g);
                    }
                }
            }

            added = true;
        } // end of adding goals

        cout << "Searching for the following conjunctive goals:\n";
        board.printPriorityQ(goals);

    } // end of getUserGoal()

};


#endif /* Game_h */

是否有发生这种情况的原因,或者我只是错过了全局?谢谢您的帮助!

c++ list vector int
1个回答
1
投票

initializer_list是C ++ 11的功能,它可能在C ++ 98中不可用(可能是您正在使用的工具链),从而导致此错误。

© www.soinside.com 2019 - 2024. All rights reserved.