我正在尝试创建两个包含自定义结构的不同向量,但当我尝试向这些向量添加元素时,"deck" 向量可以正常工作,而 "players" 向量会抛出错误。我是 C++ 新手,无法找出问题所在。
以下是它抛出的错误:
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>)'|
这是我正在使用的代码:
#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;
}
emplace_back()
似乎更能表达您的意图:players.emplace_back("Jim"s, 500, 5);
- cdonat