多维数组C ++中数组下标的无效类型'int [int]'

时间:2018-10-26 06:53:02

标签: c++ arrays pointers multidimensional-array

在做纸牌游戏课程时,出现了此错误
数组下标的类型'int [int]'无效'
这是代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>

int randomize(int, int);
int* generateCardNumber(int[5]);
int* generateCardLogo(int, int[13][4], int[5]);
int* compareCard(int[5], int[5], int[5], int[5], int[13][4]);
int printResult(int[5]);
void showCard(int, int[5], int[5], int[5], int[5]);
void printCard(int[5], int[5]);


using namespace std;

int main()
{
    srand(time(NULL));
    int player1Number[5], player1Logo[5];
    int *number1, *logo1;
    int player2Number[5], player2Logo[5];
    int *number2, *logo2;
    int cardInfo[13][4];
    int **card;
    char answer;

   do
   {
       int playerInput;
       int d, c, r;
       int player1Score, player2Score;
       cout<<"Welcome to the card game!"<<endl;
       cout<<" Choose player{1: Player 1; 2: Player 2} : ";
       cin>>playerInput;
       cout<<endl;

       do
       {
            cout<<"Press d to deal card :";
       }while(d != 'd');
       number1  = generateCardNumber(player1Number);
       logo1    = generateCardLogo(1,cardInfo, player1Number);
       for (int i = 0; i<5; i++)
       {
           cardInfo[player1Number[i]][player1Logo[i]] = 1;
       }
       number2  = generateCardNumber(player2Number);
       logo2        = generateCardLogo(2,cardInfo, player2Number);
       for (int i = 0; i<5; i++)
       {
           cardInfo[player2Number[i]][player2Logo[i]] = 1;
       }
       showCard(playerInput,player1Number,player1Logo,
       player2Number,player2Logo);

       do
       {
          cout<<"Press c to compare card :";
       }while(c != 'c');
       *card = compareCard(player1Number,player1Logo,
       player2Number,player2Logo,cardInfo);
       for (int i = 0; i<5; i++)
       {
           if(cardInfo[player1Number[i]][player1Logo[i]] = -1)
           {
               player1Number[i] = -1;
               player1Logo[i] = -1;
           }
           else if(cardInfo[player2Number[i]][player2Logo[i]] = -1)
           {
               player2Number[i] = -1;
               player2Logo[i] = -1;
           }
       }

       showCard(playerInput,player1Number,
       player1Logo,player2Number,player2Logo);

       do
       {
           cout<<"Press r to show result :";
       }while(r != 'r');
       if(playerInput == 1)
       {
           cout<<"Player1 (You):        ";
           player1Score = printResult(player1Number);
           cout<<"Player2 (Computer):   ";
           player2Score = printResult(player2Number);
           if(player1Score > player2Score) cout<<"You WIN!"<<endl;
           else cout<<"Computer WIN!"<<endl;
       }
       else if(playerInput == 2)
       {
           cout<<"Player1 (Computer):   ";
           player1Score = printResult(player1Number);
           cout<<"Player2 (You):        ";
           player2Score = printResult(player2Number);
           if(player2Score > player1Score) cout<<"You WIN!"<<endl;
           else cout<<"Computer WIN!"<<endl;
       }
       cout<<"Do you want to play again? (y/n)"<<endl;
       cin>>answer;     
   }while(answer == 'Y' || answer == 'y');
}

int randomize (int x, int y)
{
   return (rand()%y + x);
}

int* generateCardNumber (int numberArray[5])
{
    int arrayStoring[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
    for (int i = 0; i<5; i++)
    {
        do
        {
            numberArray[i] = randomize(1,13);   
        }while (arrayStoring[numberArray[i]] == 1);
        arrayStoring[numberArray[i]] = 1;
    }
    return numberArray;
}
int* generateCardLogo (int turn, int cardInfo[4][13], int player2Number[5])
{
    int logoArray[5];
    if(turn == 1)
    {
        for (int i = 0; i<5; i++)
        {
            logoArray[i] = randomize(1,4);
        }
        return logoArray;
    }
    else if(turn == 2)
    {
        for (int i = 0; i<5; i++)
        {
            do
            {
                logoArray[i] = randomize(1,4);  
            }while (cardInfo[player2Number[i]][logoArray[i]] == 1);         
        }
        return logoArray;
    }   
}

int** compareCard(int player1Number, int player1Logo, int player2Number, int player2Logo, int cardInfo)
{
    for(int i=0; i<5 ; i++)
    {
        for(int j=0; j<5 ; j++)
        {
            if(player1Number[i] == player2Number[j])
            {
                if(player1Logo[i] < player2Logo[j]) cardInfo[player1Number[i]][player1Logo[i]] = -1;
                else if(player1Logo[i] > player2Logo[j]) cardInfo[player2Number[i]][player2Logo[i]] = -1;
                break;
            }
        }
    }
    return cardInfo;
}

int printResult (int playerNumber)
{
    int playerScore = 0;
    for (int i = 0; i<5; i++)
    {
        if(playerNumber[i] == -1) break;
        else if(playerNumber[i] == 0)
        {
            playerScore += 15;  
            cout<<"15 ";
        }
        else if(playerNumber[i] > 0 && playerNumber[i] < 10 )
        {
            playerScore += (playerNumber[i] + 1);
            cout<< (playerNumber[i] + 1) <<" ";
        }
        else if(playerNumber[i] >= 10)
        {
            playerScore += 12;
            cout<<"12 ";
        }

        if(i<4) cout<<"+ ";
        else if(i==4)
        {
            cout<<"= "<<playerScore<<" points";
        }
    }
    return playerScore;
}

void printCard (int numberArray[5], int logoArray[5])
{
    for (int i = 0; i<5; i++)
    {
        switch(numberArray[i])
        {
            case -1 :
                cout<<"<fold> ";
                break;
            case 0 :
                cout<<"Ace ";
                break;
            case 1 :
                cout<<"2 ";
                break;
            case 2 :
                cout<<"3 ";
                break;
            case 3 :
                cout<<"4 ";
                break;
            case 4 :
                cout<<"5 ";
                break;
            case 5 :
                cout<<"6 ";
                break;
            case 6 :
                cout<<"7 ";
                break;
            case 7 :
                cout<<"8 ";
                break;
            case 8 :
                cout<<"9 ";
                break;
            case 9 :
                cout<<"10 ";
                break;
            case 10 :
                cout<<"Jack ";
                break;
            case 11 :
                cout<<"Queen ";
                break;
            case 12 :
                cout<<"King ";
                break;
        }
        switch(logoArray[i])
        {
            case -1:
                break;
            case 0:
                cout<<"Diamond  ";
                break;
            case 1:
                cout<<"Club     ";
                break;
            case 2:
                cout<<"Heart    ";
                break;
            case 3:
                cout<<"Spade    ";
                break;  
        }   
    }
}
void showCard (int playerInput, int player1Number, int player1Logo, int player2Number, int player2Logo)
{
    if(playerInput == 1)
    {
        cout<<"Player1 (You):"<<endl;
        printCard(player1Number, player1Logo);
        cout<<"Player2 (Computer):"<<endl;
        printCard(player2Number, player2Logo);
    }
    else if(playerInput == 2)
    {
        cout<<"Player1 (Computer):"<<endl;
        printCard(player1Number, player1Logo);
        cout<<"Player2 (You):"<<endl;
        printCard(player2Number, player2Logo);
    }
}

老实说,我还是编程新手。所以我不知道我不能从函数返回整个数组。在寻找线索之后,我发现我可以使用指针从函数返回数组。当我尝试修复代码时,会出现此错误,并且我不知道需要修复什么。 当我尝试使用函数参数中的数组时,错误会不断出现,例如

if(player1Number[i] == player2Number[j])<br/>

来自功能

int** compareCard(int player1Number, int player1Logo, int player2Number, int player2Logo, int cardInfo)

及以后。

有人可以帮我解决这个问题吗?对代码的更正将不胜感激。非常感谢你! (对不起,如果我的英语不好)

1 个答案:

答案 0 :(得分:0)

您要声明compareCard,并使用一堆数组(即int指针)作为参数:

int* compareCard(int[5], int[5], int[5], int[5], int[13][4]);

然后,您正在使用整数(不是指针)作为参数来实现它:

int** compareCard(int player1Number, int player1Logo, int player2Number, int player2Logo, int cardInfo)
{
    for(int i=0; i<5 ; i++)

您还将它们用作数组:

if(player1Logo[i] < player2Logo[j]) cardInfo[player1Number[i]][player1Logo[i]] = -1;

此外,返回类型错误。您将其声明为int*,然后将其实现为int**。您必须确保函数的签名匹配。看起来,使用声明的签名并将其应用于定义应该可以解决此编译时错误。

您的编译器是否对这些函数声明发出任何警告?如果不是,请查看是否可以设置更严格的警告。正确警告潜在错误,可以大大提高调试效率。