代码中的C++2D向量push_back错误
题
我让我的程序使用单个向量,并决定使用2D向量来表示多个手(一维向量vPlayerHand1加上一维向量vPlayerHand2plus。..).我无法弄清楚如何填充矢量。我正在使用Visual Studio C++2010,它似乎没有完全实现C++11,并在IDE中报告解析错误,用于作为本论坛中类似问题的答案提供的代码。在下面的大纲卡是一个类。
#include <vector>
std::vector<std::vector<Card>> vPlayerHand;
vPlayerHand.push_back(vShoe.back()); /* fails with parsing error No instance of
overloaded function... */
vPlayerHand[0].push_back(vShoe.back()); /* builds okay then error Debug Assertion
Failed... vector subscript out of range */
我缺少一些关于正确使用带有2D向量(向量的向量)的push_back函数的东西我明白第一个引用是行。当我用push_back填充时,它应该只是做第一行。
这里有更完整的代码:
第29行编辑。..代码按给定的方式正确运行 根据@RSahu的解决方案在第32a行重新编辑正确运行。注释掉第29行
1 # include <iostream>
2 # include <vector>
3 # include <algorithm>
4 # include <ctime>
5 # include "Card.h" //Defines Card as having Suit, Rank and functions GetSuit() GetRank()
6
7 std::vector<Card> vShoe; //Card Shoe vector holds 1-8 decks
8 std::vector<Card> vDeck; //Deck vector holds 52 cards of Card class
9 std::vector<std::vector<Card>> vPlayerHand; // Player Hands 0-original, 1-split1, n-splitn
10 std::vector<Card> vDealerHand;
11
12 void CreateDeck(); //Populates Deck with 52 Cards
13 void CreateShoe(int); //Populates Show with Decks*n number of Decks
14 void ShuffleShoe(); // uses random_shuffle
15
16 int main() {
17
18 int iDeckCount = 2;
19 const int NumPlayers = 1;
20 srand(time(0));
21
22
23 CreaateDeck();
24 CreateShoe(iDeckCount);
25 ShuffleShoe();
26
27 // Following line gives parsing error
28 // vPlayerHand = std::vector<std::vector<Card>> (5, std::vector<std::vector<Card>>(12));
// added this line and now runs as expected
/* removed this line in favor of line 32a as per @RSahu
29 vPlayerHand.resize(2); // need only initial size for 2 elements
*/
30 for (int i=0; i<=NumPlayers; i++) {
31 // I believe this is where dimension error comes vPlayerHand[0].push_back
32 // I tried vPlayerHand.push_back(vShoe.back()) but get parsing error "No instance of overloaded function.."
// This line added as per R Sahu. compiles and runs correctly
32a vPlayerHand.push_back(std::vector<Card>());
33 vPlayerHand[0].push_back(vShoe.back()); //Top card in Shoe (last card in vector) is dealt to Player
34 vShoe.pop_back(); //Top card in Shoe is removed (destroyed) from vector Shoe
35 vDealerHand.push_back(vShoe.back()); //Top card in Shoe (last card in vector) is dealt to Dealer
36 vShoe.pop_back(); //Top card in Shoe is removed (destroyed) from vector Shoe
37 }
38
39 /* Show Results
40 std::cout << "\n---------------------------------\n" ;
41 std::cout << " Players Hand" << std::endl;
42 std::cout << vPlayerHand[0][0].GetRank() << "," << vPlayerHand[0][0].GetSuit() << " ";
43 std::cout << vPlayerHand[0][1].GetRank() << "," << vPlayerHand[0][1].GetSuit() << std::endl;
44 */
45 }
任何见解都会有所帮助。
解决方案
你已经定义了 vPlayerHand
作为:
std::vector<std::vector<Card>> vPlayerHand;
当您使用 vPlayerHand.push_back(arg)
, arg
必须是类型 std::vector<Card>
或可转换至 std::vector<Card>
.类型的参数 Card
不能用作该函数的参数。这就是你在使用时所尝试的
vPlayerHand.push_back(vShoe.back())
你需要的是:
vPlayerHand.push_back(std::vector<Card>());
vPlayerHand.back().push_back(vShoe.back());
vShoe.pop_back();
vPlayerHand.back().push_back(vShoe.back());
vShoe.pop_back();
不隶属于 StackOverflow