繁体   English   中英

尝试写一个简单的 Blackjack,但有错误我不明白

[英]Trying to write a simple Blackjack, but there are errors I don't understand

我的代码有问题。 它给出了随机错误,我不知道为什么..我是 C++ 的新手,所以请耐心等待 >.>

这是有问题的代码:

while (!IsGameOver) {
    struct decktype deck = DeckInit();
    struct card card = PickACard(deck);
    PrintHand(TextCard(card));
}

无论我做什么,“PrintHand”的参数都会导致编译错误。 这是两个功能。

char *TextCard(struct card &card) {
    char str[22];
    sprintf(str,"%s of %s (%d)",card_num[card.number],card_type[card.color],card.value);
    return str;
}


struct card PrintHand(char &cardtext) {
    struct card card;
    return card;
}

PrintHand 还没有完成,但我不知道如何让它工作。基本上我想做的是从 TextCard 中输入一个字符串以在 PrintHand 中使用。 能否请你帮忙? 非常感激。

编辑:

目前的结构“卡片”看起来像这样。

struct card {
    int color;
    int number;
    int value;
    char *hand;
    int totalvalue;
};

错误是“无法将某物转换为某物”。 对不起,我不能更具体:/

PrintHand期望引用char ,但您(即TextCard )提供了指向char的指针。

有几个错误。

struct card PrintHand(char &cardtext) {
    struct card card;
    return card;
}

不使用 cardtext,但真正的错误是从 TextCard 传递给它的值是 char*,所以这应该是struct card PrintHand(char *cardtext)

然后:

char *TextCard(struct card &card) {
    char str[22];
    sprintf(str,"%s of %s (%d)",card_num[card.number],card_type[card.color],card.value);
    return str;
}

这将返回 str,即堆栈中 str[0] 的地址,并在TextCard返回后消失,因此不应使用该返回值。 为 function 中的 char 数组new一些存储返回它,并让调用者清理,或者从调用者传入一个 char 数组。

您不能创建局部变量并从 function 返回它。请改用mallocnew

char str[22];

char * str = (char *) malloc(22* sizeof(char)); OR
String str = "sometext" + "othertext";

我不知道,你想在这里做什么:

struct card PrintHand(char &cardtext) {
    struct card card;
    return card;
}

如果您只想打印文本,请执行以下操作:

void PrintHand(char * cardtext) { // * instead of &
   printf("%s", cardtext);
}

暂无
暂无

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

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