c ++代码中的2D向量push_back错误

时间:2014-07-29 03:51:20

标签: c++ vector 2d-vector

我让我的程序使用单个向量并决定使用2D向量来表示多个手(一维向量vPlayerHand1加上一维向量vPlayerHand2加...)。我无法弄清楚如何填充矢量。我正在使用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 */

我错过了正确使用push_back函数和2D矢量(矢量矢量)的东西我理解第一个参考是行。当我使用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 }

任何见解都会有所帮助。

1 个答案:

答案 0 :(得分:1)

您已将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();
相关问题