C++ 코드의 2D 벡터 push_back 오류
문제
나는 단일 벡터로 작업하는 프로그램을 가지고 있었고 여러 손을 표현하기 위해 2D 벡터를 사용하기로 결정했습니다(1차원 벡터 vPlayerHand1 + 1차원 벡터 vPlayerHand2 + ...).벡터를 채우는 방법을 알 수 없습니다.저는 C++11을 완전히 구현하지 않은 것으로 보이며 이 포럼에서 유사한 질문에 대한 답변으로 제공되는 코드에 대한 IDE의 구문 분석 오류를 보고하는 Visual Studio C++ 2010을 사용하고 있습니다.아래 개요에서 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 }
어떤 통찰력이라도 도움이 될 것입니다.
해결책
당신은 정의했습니다 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