-1

我让我的程序使用单个向量,并决定使用 2D 向量来表示多只手(一维向量 vPlayerHand1 加上一维向量 vPlayerHand2 加上 ...)。我不知道如何填充向量。我正在使用 Visual Studio C++ 2010,它似乎没有完全实现 C++11,并报告 IDE 中的代码解析错误,这些代码作为对本论坛中类似问题的答案提供。在下面的大纲中 Card 是一个类。

#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 }

任何见解都会有所帮助。

4

1 回答 1

1

您已定义vPlayerHand为:

std::vector<std::vector<Card>> vPlayerHand;

使用时vPlayerHand.push_back(arg)arg必须是 typestd::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();
于 2014-07-29T03:57:26.547 回答