C ++ 2D Errore Push_back nel codice
Domanda
Ho avuto il mio programma che lavora con un singolo vettore e ho deciso di utilizzare un vettore 2D per rappresentare molteplici mani (un vettore dimensionale VPlayerHand1 Plus One Dimensional Vector VPlayerHand2 Plus ...).Non riesco a capire come popolare il vettore.Sto usando Visual Studio C ++ 2010 che non sembra implementare completamente C ++ 11 e segnala errori analizzativi nell'IDE per il codice fornito come risposte a domande simili in questo forum.Nel contorno della carta qui sotto è una classe.
#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 */
.
Mi manca qualcosa sull'uso corretto della funzione Push_back con un vettore 2D (vettore dei vettori) capisco che il primo riferimento è alla riga.E quando popolo con Push_back dovrebbe semplicemente fare la prima riga.
Ecco il codice più completo:
Modificato alla linea 29 ... Il codice funziona correttamente come indicato Re-modificato alla linea 32a come da soluzione di @Rsahu corre correttamente.Commentato la riga 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 }
.
Qualsiasi intuizione sarebbe utile.
Soluzione
Hai definito vPlayerHand
come:
std::vector<std::vector<Card>> vPlayerHand;
.
Quando si utilizza vPlayerHand.push_back(arg)
, arg
deve essere di tipo std::vector<Card>
o convertibile in std::vector<Card>
.Un argomento del tipo Card
non può essere utilizzato come argomento a tale funzione.Questo è quello che stai provando quando usi
vPlayerHand.push_back(vShoe.back())
.
Quello di cui hai bisogno è:
vPlayerHand.push_back(std::vector<Card>());
vPlayerHand.back().push_back(vShoe.back());
vShoe.pop_back();
vPlayerHand.back().push_back(vShoe.back());
vShoe.pop_back();
.