从“const char*”到“char”/struct 中未初始化的 const 成员的无效转换

invalid conversion from ‘const char*’ to ‘char’ / uninitialized const member in struct

我开始练习用 C++ 编程(我知道一点),所以我开始写一个简单,类似炉石的纸牌游戏,只是为了练习。一切都很好,但现在,我遇到了一个错误。 这是我的代码:

页眉:

#ifndef DRAW_H_INCLUDED
#define DRAW_H_INCLUDED

struct Card
{
    char name;
    int mana;
    int attack;
    int defense;
};

Card deck_p1[32];
Card deck_p2[32];

int cardsLeft_p1 = 30;
int cardsLeft_p2 = 30;

struct BattleG
    {
        Card p1_bg1;
        Card p1_bg2;
        Card p1_bg3;
        Card p1_bg4;
        Card p1_bg5;
        Card p2_bg1;
        Card p2_bg2;
        Card p2_bg3;
        Card p2_bg4;
        Card p2_bg5;
    };

BattleG battleG;

struct Player
{
    char name;
    int hp;
    int mana;
    Card hand1;
    Card hand2;
    Card hand3;
    Card hand4;
    Card hand5;
    Card hand6;
    Card hand7;
};

#endif // DRAW_H_INCLUDED

主要:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include "draw.h"

using namespace std;

int getRandomNumber(int min, int max)
{
    static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
    return static_cast<int>(rand() * fraction * (max - min + 1) + min);
}

Card drawCard(int p)
{
    if (p == 1)
    {
        int currentCard = getRandomNumber(1, cardsLeft_p1);
        Card drawnCard = deck_p1[currentCard];
        for (int i = currentCard; i < cardsLeft_p1; i++)
            deck_p1[i] = deck_p1[i + 1];
        cardsLeft_p1--;
        return drawnCard;
    }
    else
    {
        int currentCard = getRandomNumber(1, cardsLeft_p2);
        Card drawnCard = deck_p2[currentCard];
        for (int i = currentCard; i < cardsLeft_p2; i++)
            deck_p2[i] = deck_p2[i + 1];
        cardsLeft_p2--;
        return drawnCard;
    }
}

void newPage()
{
    for (int i = 0; i < 25; i++)
        cout << endl;
}

void printDetails(Card player_hand)
{
    cout << "Attack: " << player_hand.attack << endl;
    cout << "Defense: " << player_hand.defense << endl;
    cout << "Mana cost: " << player_hand.mana << endl;
}

void printBattleG(Player player1, Player player2, BattleG battleG)
{
    cout << endl << "Current cards on the battleground:\n" << endl;
    cout << player1.name << "'s first card on the battleground: " << battleG.p1_bg1.name << endl;
    cout << player1.name << "'s second card on the battleground: " << battleG.p1_bg2.name << endl;
    cout << player1.name << "'s third card on the battleground: " << battleG.p1_bg3.name << endl;
    cout << player1.name << "'s fourth card on the battleground: " << battleG.p1_bg4.name << endl;
    cout << player1.name << "'s fifth card on the battleground: " << battleG.p1_bg5.name << endl;
    cout << player2.name << "'s first card on the battleground: " << battleG.p2_bg1.name << endl;
    cout << player2.name << "'s second card on the battleground: " << battleG.p2_bg2.name << endl;
    cout << player2.name << "'s third card on the battleground: " << battleG.p2_bg3.name << endl;
    cout << player2.name << "'s fourth card on the battleground: " << battleG.p2_bg4.name << endl;
    cout << player2.name << "'s fifth card on the battleground: " << battleG.p2_bg5.name << endl;
}

void useCard(Player player, Card nothing)
{
    cout << endl << "Which card do you want to use? (Write the card's name or 'none')" << endl;
    char choise;
    cin >> choise;

    if (choise == player.hand1.name)
    {
        if (player.hand1.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand1;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand1;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand1;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand1;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand1;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand1.mana;
        player.hand1 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand2.name)
    {
        if (player.hand2.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand2;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand2;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand2;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand2;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand2;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand2.mana;
        player.hand2 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand3.name)
    {
        if (player.hand3.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand3;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand3;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand3;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand3;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand3;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand3.mana;
        player.hand3 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand4.name)
    {
        if (player.hand4.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand4;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand4;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand4;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand4;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand4;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand4.mana;
        player.hand4 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand5.name)
    {
        if (player.hand5.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand5;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand5;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand5;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand5;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand5;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand5.mana;
        player.hand5 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand6.name)
    {
        if (player.hand6.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand6;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand6;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand6;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand6;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand6;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand6.mana;
        player.hand6 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }

    else if (choise == player.hand7.name)
    {
        if (player.hand7.mana <= player.mana)
        {
            if (battleG.p1_bg1.name == nothing.name)
            {
                battleG.p1_bg1 = player.hand7;
                cout << "First card on your side of the battleground: " << battleG.p1_bg1.name << endl;
            }
            else if (battleG.p1_bg2.name == nothing.name)
            {
                battleG.p1_bg2 = player.hand7;
                cout << "Second card on your side of the battleground: " << battleG.p1_bg2.name << endl;
            }
            else if (battleG.p1_bg3.name == nothing.name)
            {
                battleG.p1_bg3 = player.hand7;
                cout << "Third card on your side of the battleground: " << battleG.p1_bg3.name << endl;
            }
            else if (battleG.p1_bg4.name == nothing.name)
            {
                battleG.p1_bg4 = player.hand7;
                cout << "Fourth card on your side of the battleground: " << battleG.p1_bg4.name << endl;
            }
            else if (battleG.p1_bg5.name == nothing.name)
            {
                battleG.p1_bg5 = player.hand7;
                cout << "Fifth card on your side of the battleground: " << battleG.p1_bg5.name << endl;
            }
        player.mana = player.mana - player.hand7.mana;
        player.hand7 = nothing;
        }
        else
            cout << "You don't have enough mana." << endl;
    }
}

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    rand();

    Card card1 = {"Card1", 1, 1, 1};
    Card card2 = {"Card2", 2, 2, 2};
    Card card3 = {"Card3", 2, 3, 1};
    Card card4 = {"Card4", 2, 1, 3};
    Card card5 = {"Card5", 3, 3, 3};
    Card card6 = {"Card6", 3, 4, 2};
    Card card7 = {"Card7", 3, 5, 1};
    Card card8 = {"Card8", 3, 2, 4};
    Card card9 = {"Card9", 3, 1, 5};
    Card card10 = {"Card10", 4, 4, 4};
    Card card11 = {"Card11", 4, 5, 3};
    Card card12 = {"Card12", 4, 6, 2};
    Card card13 = {"Card13", 4, 7, 1};
    Card card14 = {"Card14", 4, 3, 5};
    Card card15 = {"Card15", 4, 2, 6};
    Card card16 = {"Card16", 4, 1, 7};
    Card card17 = {"Card17", 5, 5, 5};
    Card card18 = {"Card18", 5, 6, 4};
    Card card19 = {"Card19", 5, 7, 3};
    Card card20 = {"Card20", 5, 8, 2};
    Card card21 = {"Card21", 5, 9, 1};
    Card card22 = {"Card22", 5, 4, 6};
    Card card23 = {"Card23", 5, 3, 7};
    Card card24 = {"Card24", 5, 2, 8};
    Card card25 = {"Card25", 5, 1, 9};
    Card card26 = {"Card26", 1, 2, 2};
    Card card27 = {"Card27", 2, 4, 4};
    Card card28 = {"Card28", 3, 6, 6};
    Card card29 = {"Card29", 4, 8, 8};
    Card card30 = {"Card30", 5, 10, 10};
    Card nothing = {"Empty"};

    deck_p1[1] = card1;
    deck_p1[2] = card2;
    deck_p1[3] = card3;
    deck_p1[4] = card4;
    deck_p1[5] = card5;
    deck_p1[6] = card6;
    deck_p1[7] = card7;
    deck_p1[8] = card8;
    deck_p1[9] = card9;
    deck_p1[10] = card10;
    deck_p1[11] = card11;
    deck_p1[12] = card12;
    deck_p1[13] = card13;
    deck_p1[14] = card14;
    deck_p1[15] = card15;
    deck_p1[16] = card16;
    deck_p1[17] = card17;
    deck_p1[18] = card18;
    deck_p1[19] = card19;
    deck_p1[20] = card20;
    deck_p1[21] = card21;
    deck_p1[22] = card22;
    deck_p1[23] = card23;
    deck_p1[24] = card24;
    deck_p1[25] = card25;
    deck_p1[26] = card26;
    deck_p1[27] = card27;
    deck_p1[28] = card28;
    deck_p1[29] = card29;
    deck_p1[30] = card30;

    deck_p2[1] = card1;
    deck_p2[2] = card2;
    deck_p2[3] = card3;
    deck_p2[4] = card4;
    deck_p2[5] = card5;
    deck_p2[6] = card6;
    deck_p2[7] = card7;
    deck_p2[8] = card8;
    deck_p2[9] = card9;
    deck_p2[10] = card10;
    deck_p2[11] = card11;
    deck_p2[12] = card12;
    deck_p2[13] = card13;
    deck_p2[14] = card14;
    deck_p2[15] = card15;
    deck_p2[16] = card16;
    deck_p2[17] = card17;
    deck_p2[18] = card18;
    deck_p2[19] = card19;
    deck_p2[20] = card20;
    deck_p2[21] = card21;
    deck_p2[22] = card22;
    deck_p2[23] = card23;
    deck_p2[24] = card24;
    deck_p2[25] = card25;
    deck_p2[26] = card26;
    deck_p2[27] = card27;
    deck_p2[28] = card28;
    deck_p2[29] = card29;
    deck_p2[30] = card30;

    BattleG battleG;

    Player player1;
    cout << "Player1! What's your name?\n";
    cin >> player1.name;
    player1.hp = 30;
    player1.mana = 1;

    Player player2;
    cout << "Player2! What's your name?\n";
    cin >> player2.name;
    player2.hp = 30;
    player2.mana = 1;

    player1.hand1 = drawCard(1);
    player1.hand2 = drawCard(1);
    player1.hand3 = drawCard(1);
    player1.hand4 = drawCard(1);
    player1.hand5 = drawCard(1);
    player1.hand6 = nothing;
    player1.hand7 = nothing;

    newPage();
    cout << player1.name << "'s turn:\n" << endl;

    cout << endl << "Your first card is: " << player1.hand1.name << endl;
    printDetails(player1.hand1);
    cout << endl << "Your second card is: " << player1.hand2.name << endl;
    printDetails(player1.hand2);
    cout << endl << "Your third card is: " << player1.hand3.name << endl;
    printDetails(player1.hand3);
    cout << endl << "Your fourth card is: " << player1.hand4.name << endl;
    printDetails(player1.hand4);
    cout << endl << "Your fifth card is: " << player1.hand5.name << endl;
    printDetails(player1.hand5);

    battleG.p1_bg1 = nothing;
    battleG.p1_bg2 = nothing;
    battleG.p1_bg3 = nothing;
    battleG.p1_bg4 = nothing;
    battleG.p1_bg5 = nothing;
    battleG.p2_bg1 = nothing;
    battleG.p2_bg2 = nothing;
    battleG.p2_bg3 = nothing;
    battleG.p2_bg4 = nothing;
    battleG.p2_bg5 = nothing;

    useCard(player1, nothing);

    player2.hand1 = drawCard(2);
    player2.hand2 = drawCard(2);
    player2.hand3 = drawCard(2);
    player2.hand4 = drawCard(2);
    player2.hand5 = drawCard(2);
    player2.hand6 = nothing;
    player2.hand7 = nothing;

    newPage();
    cout << player2.name << "'s turn:\n";

    cout << endl << "Your first card is: " << player2.hand1.name << endl;
    printDetails(player2.hand1);
    cout << endl << "Your second card is: " << player2.hand2.name << endl;
    printDetails(player2.hand2);
    cout << endl << "Your third card is: " << player2.hand3.name << endl;
    printDetails(player2.hand3);
    cout << endl << "Your fourth card is: " << player2.hand4.name << endl;
    printDetails(player2.hand4);
    cout << endl << "Your fifth card is: " << player2.hand5.name << endl;
    printDetails(player2.hand5);

    printBattleG(player1, player2, battleG);

    cout << endl << "Which card do you want to use? (Write the card's name or 'none')" << endl;
    cin >> choise;

    return 0;
}

错误是:在卡片定义中从“const char”到“char”* 的转换无效(Card card1 = {"Card1", 1, 1, 1};Card card2 = {"Card1", 1, 1, 1};、等)和结构const char name;(header/4st行)未初始化的const成员如果我将char重写为const字符。 我搜索了很多有关此错误的信息,但 none 的解决方案对我有用。我有一个早期的代码可以正常工作,但我不能在这里复制它,因为它超出了字符限制。我在 CodeBlocks 16.01 中使用 GNU GCC 编译器。

const char*char 的错误消息无效转换来自 struct Card 的声明。

应该是

struct Card
{
    const char* name;
    ...;
};

您的变量 choise 可以是 std::string 类型。进一步的字符串比较就可以了。

跳出两个相关的语法问题:

struct Card
{
    char name;
    int mana;
    int attack;
    int defense;
};

Card::name 是单个字符。随着

(Card card1 = {"Card1", 1, 1, 1};

OP 试图将指向多个字符(几乎是 char *。稍后我将对此进行扩展)的指针放置到单个字符中。类型差异太大,无法赋值

第一个想到的方案大概是

struct Card
{
    char * name;
    int mana;
    int attack;
    int defense;
};

但是由于问题 2 这将不起作用。例如,字符串文字 "Card1" 可能存储也可能不存储在内存的可写区域中。因此,字符串文字总是 const 以确保编译器在您尝试写入时会发现错误。

所以 struct Card 可以定义为

struct Card
{
    const char * name;
    int mana;
    int attack;
    int defense;
};

但这是 C++,所以

struct Card
{
    std::string name;
    int mana;
    int attack;
    int defense;
};

将是您的首选,并且对您有益,因为您进行了很多字符串比较,这些比较可以编译,但最终 不起作用 ,如 const char * 所写。

两者都将导致大量其他语法错误被揭示。一方面

char choise;

需要

std::string choise;

除了建议 OP 熟悉他们的 development environment's debugger.

之外,我不打算解决此代码中的逻辑错误

建议:不要在没有测试的情况下编写这么多代码。您所做的假设无法通过测试的可能性非常大,您将不得不放弃大量工作——不管是行不通的代码还是基于行不通的代码构建的代码工作。早犯的错误会留下很长的阴影,所以要早点测试并经常测试。

编码愉快!