如何解决在函数外使用向量值
背景
我们正在创建一个二十一点游戏,我们想向玩家发牌。我们已经创建了一个创建数字向量的函数,并且我们还能够“洗牌”这些数字。我们现在要做的是将向量 shuffled_cardvals 分配给庄家和玩家。
我们的问题是我们无法让 shuffled_cardvals 在函数之外工作。通过仔细阅读其他帖子,我们相当确定我们的问题是因为 shuffled_cardvals 在函数 shuffle() 内部使用,所以它不存在于该函数之外。
因此,我们知道问题所在,但不知道如何解决。
这是我们的代码。我们使用了一个源文件和一个头文件,因为我们认为这会让事情变得更简洁。
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
#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,3,4,5,6,7,8,9,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;
}
控制台输出(21 年 1 月 4 日编辑/修正)
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] << " ";} ^~~~~~~~~~~~~
|
免责声明
我和我的朋友试图在寒假期间一起开始学习 C++,而我们完全是菜鸟。现在的代码处于非常未完成的状态,我们确信它已经存在很多问题。任何帮助表示赞赏!
解决方法
好的,我有点担心。
-
std::vector <int> cardvals
和std::vector <std::string> cards
在您可能不想要的标头中初始化。我建议将其声明为extern
并在您的.cpp
中对其进行初始化。这样做的原因是每个翻译单元都会得到自己的数组实例,这会让人头疼:) -
可以全局声明这两个数组,但通常不推荐。我会创建函数来创建这样的数组,但它的方式很好。
-
您想通过
std::vector <int> &x;
实现什么目标。我相信std::vector <int> x;
会解决您的问题 :)
既然您要使用 C++ 而不是 C,您还应该编写更多面向对象的东西,而不是在全局范围内声明一些东西,这被一些人认为是一个坏习惯。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。