在函数外使用矢量值
Using vector values outside of a function
背景
我们正在创建一个 21 点游戏,我们想要向玩家发牌。我们已经创建了一个创建数字向量的函数,而且我们还能够“打乱”这些数字。我们现在要做的是获取该向量 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, 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;
}
控制台输出(Edited/Fixed 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] << " ";} ^~~~~~~~~~~~~
|
免责声明
我和我的朋友正试图在寒假期间一起开始学习 C++,我们完全是菜鸟。现在的代码处于非常未完成的状态,我们确信它已经有很多问题。感谢您的帮助!
好吧,有些事情我有点担心。
std::vector <int> cardvals
和 std::vector <std::string> cards
在 header 中初始化,您可能不想要。我建议将其声明为 extern
并在您的 .cpp
中对其进行初始化。这样做的原因是每个翻译单元都会得到他们自己的数组实例,这会让人头疼:)
可以全局声明两个数组,但通常不推荐。我会创建函数来创建这样的数组,但它很好。
你想用 std::vector <int> &x;
达到什么目的?我相信 std::vector <int> x;
会解决您的问题:)
由于您要使用 C++ 而不是 C,因此您还应该编写更多面向 object 的东西,而不是全局声明一些人认为是坏习惯的东西。
背景
我们正在创建一个 21 点游戏,我们想要向玩家发牌。我们已经创建了一个创建数字向量的函数,而且我们还能够“打乱”这些数字。我们现在要做的是获取该向量 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, 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;
}
控制台输出(Edited/Fixed 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] << " ";} ^~~~~~~~~~~~~
|
免责声明
我和我的朋友正试图在寒假期间一起开始学习 C++,我们完全是菜鸟。现在的代码处于非常未完成的状态,我们确信它已经有很多问题。感谢您的帮助!
好吧,有些事情我有点担心。
std::vector <int> cardvals
和std::vector <std::string> cards
在 header 中初始化,您可能不想要。我建议将其声明为extern
并在您的.cpp
中对其进行初始化。这样做的原因是每个翻译单元都会得到他们自己的数组实例,这会让人头疼:)可以全局声明两个数组,但通常不推荐。我会创建函数来创建这样的数组,但它很好。
你想用
std::vector <int> &x;
达到什么目的?我相信std::vector <int> x;
会解决您的问题:)
由于您要使用 C++ 而不是 C,因此您还应该编写更多面向 object 的东西,而不是全局声明一些人认为是坏习惯的东西。