Error of vector vectors C ++

I try to make two different vectors containing custom structures, but when I try to add elements to the vectors that it works for the deck vector, but it throws a player error. I am new to C ++ and cannot understand what is wrong.

These errors are:

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|

error: no matching function for call to 'std::vector<BlackjackClass::player>::push_back(<brace-enclosed initializer list>)'|

This is the code I'm using:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class BlackjackClass {

    private:
        struct card
        {
                string label;
                int value;
                string suit;
        };
        vector<card> deck;

        struct player
        {
                string name;
                int bankroll;
                int default_bet = 5;
        };
        vector<player> players;

    public:
        BlackjackClass()
        {
            // Works
            deck.push_back({"Queen", 10, "Hearts"});
            // Doesn't Work
            players.push_back({"Jim", 500, 5});

        }
};

int main()
{
    BlackjackClass Blackjack;
}
+4
source share
3 answers

This is because you have a default value for default_bet . Delete it and it will work or build the object explicitly instead of the initialization list

    struct player
    {
            string name;
            int bankroll;
            int default_bet = 5;
            player(string name_, int bankroll_, int default_bet_)
            {
                name=name_;
                bankroll=bankroll_;
                default_bet_=default_bet_;
            }
    };


    players.push_back(player("Jim", 500, 5));
+1
source

I do not know your compilation options, but a warning

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|

, ++ 11 ( ) .

, , initialzer apperently . :

int default_bet = 5;

++ 11, .

+1

, :

card c { "Queen", 10, "Hearts" };    // OK     (g++ -std=c++11)
player p { "Jim", 500, 5 };          // Not OK (g++ -std=c++11)

, , , , . ; . player card , , .


, -, card , player not.

++ 11 , N3337 [dcl.init.aggr]/1:

- ( 9) , (12.1), (9.2), ( 11), ( 10) (10.3).

++ 14 (N3936) :

- ( 9) - , (12.1), ( 11), ( 10) (10.3).

= 5 - , , ++ 11 player , ++ 14 player - .

g++ , g++ 5.1 - -std=c++11 -std=c++14. g++ 4.9.2 -std=c++14, g++.


: g++ 5.1 ( , ++ 14), -std=c++14 . .

+1

Source: https://habr.com/ru/post/1614654/


All Articles