You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在代码其他函数中引用C++类中定义的数组与vector?

问题根源
  • deck、suit这些是Cards类的非静态成员变量,属于类的实例(对象),不是全局变量,独立的DrawCard函数既没有访问权限,也不知道要访问哪个对象的成员,自然读不到。
  • 另外DrawCard里每次调用都执行srand(time(0))会频繁重置随机种子,短时间内多次调用的话,time(0)返回的秒数相同,生成的随机数会重复。
修复方案

方案1:把DrawCard改成Cards类的成员函数

抽卡逻辑本来就属于卡牌类的功能,这是最合理的写法:

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

using namespace std;

class Cards {
public:
    // 补全deck数组的Q、K,凑够13个元素
    string deck[13] = {"1", "2", "3", "4",  "5", "6",
                       "7", "8", "9", "10", "J", "Q", "K"};
    string suit[4] = {"Hearts", "Spades", "Diamonds", "Clubs"};

    vector<string> cardVal = {"Ace",  "Two",   "Three", "Four", "Five",
                              "Six",  "Seven", "Eight", "Nine", "Ten",
                              "Jack", "Queen", "King"};
    vector<string> suitvec = {"Hearts", "Spades", "Diamonds", "Clubs"};

    int DrawCard() {
        // 只初始化一次随机种子
        static bool seeded = false;
        if (!seeded) {
            srand(time(0));
            seeded = true;
        }

        int RandomDraw = rand() % 13;
        int RandomSuit = rand() % 4;
        int handValue = 0;

        cout << deck[RandomDraw] << " of " << suit[RandomSuit] << endl;

        // 示例:计算手牌价值
        if (RandomDraw >= 9) { // J/Q/K算10
            handValue = 10;
        } else if (RandomDraw == 12) { // A算11(可后续调整)
            handValue = 11;
        } else {
            handValue = RandomDraw + 1;
        }

        return handValue;
    }
};

// 使用示例
int main() {
    Cards myCards;
    int value = myCards.DrawCard();
    cout << "手牌价值:" << value << endl;
    return 0;
}

方案2:给DrawCard传入Cards对象

如果不想把DrawCard改成成员函数,可以让它接收一个Cards实例作为参数:

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

using namespace std;

class Cards {
public:
    string deck[13] = {"1", "2", "3", "4",  "5", "6",
                       "7", "8", "9", "10", "J", "Q", "K"};
    string suit[4] = {"Hearts", "Spades", "Diamonds", "Clubs"};

    vector<string> cardVal = {"Ace",  "Two",   "Three", "Four", "Five",
                              "Six",  "Seven", "Eight", "Nine", "Ten",
                              "Jack", "Queen", "King"};
    vector<string> suitvec = {"Hearts", "Spades", "Diamonds", "Clubs"};
};

int DrawCard(Cards& cards) {
    static bool seeded = false;
    if (!seeded) {
        srand(time(0));
        seeded = true;
    }

    int RandomDraw = rand() % 13;
    int RandomSuit = rand() % 4;
    int handValue = 0;

    // 通过传入的对象访问类成员
    cout << cards.deck[RandomDraw] << " of " << cards.suit[RandomSuit] << endl;

    // 计算手牌价值逻辑同上
    if (RandomDraw >= 9) {
        handValue = 10;
    } else if (RandomDraw == 12) {
        handValue = 11;
    } else {
        handValue = RandomDraw + 1;
    }

    return handValue;
}

// 使用示例
int main() {
    Cards myCards;
    int value = DrawCard(myCards);
    cout << "手牌价值:" << value << endl;
    return 0;
}

方案3:把类成员改成静态(不推荐)

如果所有Cards对象都共享同一套卡牌,可以把成员改成静态,这样不需要对象就能访问,但灵活性差:

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

using namespace std;

class Cards {
public:
    // 静态成员声明
    static string deck[13];
    static string suit[4];
    static vector<string> cardVal;
    static vector<string> suitvec;
};

// 静态成员必须在类外初始化
string Cards::deck[13] = {"1", "2", "3", "4",  "5", "6",
                          "7", "8", "9", "10", "J", "Q", "K"};
string Cards::suit[4] = {"Hearts", "Spades", "Diamonds", "Clubs"};
vector<string> Cards::cardVal = {"Ace",  "Two",   "Three", "Four", "Five",
                                 "Six",  "Seven", "Eight", "Nine", "Ten",
                                 "Jack", "Queen", "King"};
vector<string> Cards::suitvec = {"Hearts", "Spades", "Diamonds", "Clubs"};

int DrawCard() {
    static bool seeded = false;
    if (!seeded) {
        srand(time(0));
        seeded = true;
    }

    int RandomDraw = rand() % 13;
    int RandomSuit = rand() % 4;
    int handValue = 0;

    // 通过类名访问静态成员
    cout << Cards::deck[RandomDraw] << " of " << Cards::suit[RandomSuit] << endl;

    // 计算手牌价值逻辑同上
    if (RandomDraw >= 9) {
        handValue = 10;
    } else if (RandomDraw == 12) {
        handValue = 11;
    } else {
        handValue = RandomDraw + 1;
    }

    return handValue;
}
额外注意点
  • 你原来的deck数组只定义了12个元素,少了"Q"和"K",会导致数组越界,必须补全。
  • 随机种子srand(time(0))只需要初始化一次,用静态变量控制执行次数即可,避免重复重置导致随机数重复。

内容的提问来源于stack exchange,提问作者Cas Cusic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:01:32