简体   繁体   English

在 function 之外使用向量值

[英]Using vector values outside of a function

Background背景

We're creating a blackjack game, and we want to deal cards to a player.我们正在创建一个二十一点游戏,我们想向玩家发牌。 We've made a function that creates a vector of numbers, and we've also been able to "shuffle" those numbers.我们制作了一个 function 来创建数字向量,并且我们还能够“洗牌”这些数字。 What we want to do now is take that vector shuffled_cardvals and distribute the values to the dealer and the player.我们现在要做的是获取向量 shuffled_cardvals 并将值分配给庄家和玩家。

Our problem is that we can't get shuffled_cardvals to work outside of the function.我们的问题是我们不能让 shuffled_cardvals 在 function 之外工作。 From perusing other posts, we're fairly certain our issue is that because shuffled_cardvals is used inside the function shuffle(), it doesn't exist outside of that function.通过阅读其他帖子,我们相当确定我们的问题是因为在 function shuffle() 内部使用了 shuffled_cardvals,所以它在 function 之外不存在。

As such, we know the problem, but we can't figure out how to fix it.因此,我们知道问题所在,但我们不知道如何解决它。

Code代码

Here's our code.这是我们的代码。 We're using a source file and a header file, as we thought that would make things a bit cleaner.我们正在使用源文件和 header 文件,因为我们认为这会使事情变得更清晰。

main.cpp主文件

 #include <iostream>
 #include <cmath>
 #include <string>
 #include <vector>
 #include <algorithm>
 #include <sstream>
 #include "blackjack_header_functions.hpp"
 #include "BlackjackClass.hpp"

int main() {

//extern int choice;
// Intro
std::cout << "Good Evening. This is Samuel L. Jackson. Welcome to BlackJack(son). \n";

// output initial face value of cards
std::cout << "Initial face values for each player: ";

int facevalue = 0;
std::cout << "\n";

//shuffle the cards and output the results
shuffle();


// take the player to "the hub"
selectionMenu();

//std::cout << "your choice is " << choice << " biatch\n";

 return 0;
}

blackjack_header_functions.hpp blackjack_header_functions.hpp

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>

void selectionMenu();
void DetermineWinner();
void shuffle();

std::vector <std::string> cards = { "AC" , "AH" , "AS" , "AD" ,"KC" , "KH" , "KS" , "KD" , 
"QC" , "QH" , "QS" , "QD", "JC" , "JH" , "JS" , "JD" , "10C" , "10H" , "10S" , "10D" ,
"9C" , "9H" , "9S" , "9D" ,"8C" , "8H" , "8S" , "8D" , "7C" , "7H" , "7S" , "7D" , 
"6C" , "6H" , "6S" , "6D" , "5C" , "5H" , "5S" , "5D" , "4C" , "4H" , "4S" , "4D" , 
"3C" ,"3H" , "3S" , "3D" , "2C" , "2H" , "2S" , "2D" };


std::vector <int> cardvals = {
 2, 2, 2, 2,
 3, 3, 3, 3, 
 4, 4, 4, 4,
 5, 5, 5, 5,  
 6, 6, 6, 6,
 7, 7, 7, 7, 
 8, 8, 8, 8,
 9, 9, 9, 9, 
 10, 10, 10, 10,
 10, 10, 10, 10,
 10, 10, 10, 10,
 10, 10, 10, 10,};



std::vector <int> shuffled_cardvals = {};


std::vector <int> &x;

void shuffle(){

// First, generate a random seed for the card shuffling process.
// If we don't do this step, every time we call random_shuffle(),
// we'll get the same order of cards for our shuffled deck, making
// it not very random.

std::srand(std::time(0));

// Next, shuffle the cards and their face values
std::random_shuffle(cards.begin(), cards.end());
std::random_shuffle(cardvals.begin(), cardvals.end());

// then, put the shuffled cards in a new vector called shuffled_deck
for(int i =  0; i < cards.size(); i++) {shuffled_deck.push_back(cards[i]);}

// do it for the face values too
std::vector <int> shuffled_cardvals = {};
for(int i =  0; i < cardvals.size(); i++) {shuffled_cardvals.push_back(cardvals[i]);}

// finally, output the shuffled deck to the console
std::cout << "Shuffled deck: \n";
for(int i =  0; i < shuffled_deck.size(); i++)  {std::cout << shuffled_deck[i] << " ";}

std::cout << "\n" << "Shuffled card values: \n";
for(int i =  0; i < shuffled_cardvals.size(); i++)  {std::cout << shuffled_cardvals[i] << " ";}

 }

   void Deal(std::vector <int> &x) {

    int dealerhand = 0;
    int playerhand = 0;

    for(int i = 0; i < 4; i++) {

    dealerhand = dealerhand + x[i];
    playerhand = playerhand + x[i+1];

    }

    std::cout << "\n" << dealerhand << "\n" << playerhand;

    }

Console Output (Edited/Fixed 1/4/21)控制台 Output(编辑/修复 1/4/21)

      In file included from main.cpp:100:
 blackjack_header_functions.hpp:38:20: error: 'x' declared as reference but 
 not initialized
    38 | std::vector <int> &x;
       |                    ^
 blackjack_header_functions.hpp: In function 'void shuffle()':
 blackjack_header_functions.hpp:54:45: error: 'shuffled_deck' was not 
 declared in this scope
    54 |     for(int i =  0; i < cards.size(); i++) 
              {shuffled_deck.push_back(cards[i]);}
       |        ^~~~~~~~~~~~~
 blackjack_header_functions.hpp:62:25: error: 'shuffled_deck' was not 
 declared in this scope
    62 |     for(int i =  0; i < shuffled_deck.size(); i++)  {std::cout << 
         shuffled_deck[i] << " ";} ^~~~~~~~~~~~~
       |                         

Disclaimer免责声明

My friend and I are trying to start learning C++ together over winter break, and we're complete noobs.我和我的朋友正在尝试在寒假期间一起开始学习 C++,我们完全是菜鸟。 The code right now is in a very unfinished state, and we're sure it already has plenty of issues.现在的代码在一个非常未完成的 state 中,我们确信它已经有很多问题。 Any help is appreciated!任何帮助表示赞赏!

Okay a few things which I am kinda worried about.好的,我有点担心。

  1. std::vector <int> cardvals and std::vector <std::string> cards is initialised in the header which you probably dont want. std::vector <int> cardvalsstd::vector <std::string> cards在您可能不想要的 header 中初始化。 I would recommend declaring it as extern and initialising it in your .cpp .我建议将其声明为extern并在您的.cpp中对其进行初始化。 The reason for this is that each translation unit would get their own instance of that array which will cause headache:)这样做的原因是每个翻译单元都会获得自己的该数组实例,这将导致头痛:)

  2. It is possible to declare the two arrays globally but usually not recommended.可以全局声明两个 arrays 但通常不推荐。 I would create functions which will create such arrays but it is fine the way it is.我将创建将创建这样的 arrays 的函数,但它的方式很好。

  3. What are you trying to achieve with std::vector <int> &x;你想用std::vector <int> &x;实现什么? . . I am sure std::vector <int> x;我确定std::vector <int> x; will solve your problems:)将解决您的问题:)

Since you are going for C++ instead of C, you should also write the things a bit more object orientated instead of declaring things globally which a few people consider a bad habit.由于您要使用 C++ 而不是 C,因此您还应该多写一些 object 导向的东西,而不是在全球范围内声明一些人认为不好的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM