试图传递结构数组c ++ [关闭]

问题描述 投票:-2回答:1

[我试图为棒球运动员提供一个结构,并创建了一个包含10个结构的数组,然后我试图将该数组传递给函数lookUpPlayer,但出现前向声明错误。

这是代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int lookUpPlayer(struct playerType playersAr[]);
int MainMenu();

const int AR_SIZE = 10;
int main()
{

int index;
int userChoice;
char userError;
ifstream inFile;
ofstream outFile;

struct playerType
{
    string playerName;
    string playerPosition;
    int    playerTD;
    int    playerCatches;
    int    passingYds;
    int    receivingYds;
    int    rushingYds;
};
inFile.open("input.txt");
playerType playersAr[AR_SIZE];
index = 0;
while(inFile && index < AR_SIZE)
{
    getline(inFile, playersAr[index].playerName);
    inFile.ignore(10000, '\n');
    getline(inFile, playersAr[index].playerPosition);
    inFile.ignore(10000, '\n');
    inFile >> playersAr[index].playerTD;
    inFile >> playersAr[index].playerCatches;
    inFile >> playersAr[index].passingYds;
    inFile >> playersAr[index].receivingYds;
    inFile >> playersAr[index].rushingYds;
    index++;
}
inFile.close();
userChoice = MainMenu();



return 0;
}


int MainMenu()
{   
int userChoice;
bool invalid;
system("cls");
cout << "*****************************************\n";
cout << "*       Assignment: A5 Structs          *\n";
cout << "*                                       *\n";
cout << "*       1.  Look Up a Player            *\n";
cout << "*       2.  Edit a Player               *\n";
cout << "*       3.  Print Team Roster           *\n";
cout << "*       4.  Exit                        *\n";
cout << "*****************************************\n";

do
{
    cout << "Enter your Choice: ";
    cin >> userChoice;
    cin.clear();
    cin.ignore(10000, '\n');
    invalid = (userChoice < 1) || (userChoice > 4);
    if(cin.fail() || invalid)
    {
        //userError = getch();
        //cout << "userError: " << userError << endl;
        //cout << "userChoice: " << userChoice << endl;
        cout << "*****Invalid choice. Please enter a number in the range 1-4*****\n";
    }
} while(invalid);
return userChoice;
}

int lookUpPlayer(struct playerType playersAr[])
{
    int index;
    int found;
    string searchedPlayer;
    index =0;
    cout << "Please enter the name of the player: ";
    cin  >> searchedPlayer;
    for(index = 0; index < AR_SIZE; index++)
    {
        found = playersAr[index].name.find(searchedPlayer);
        if(found != string::npos)
        {
            cout << "player found\n";
            return index;
        }           
    }
    return -1;

}

这是我得到的错误:

 error: invalid use of incomplete type 'struct playerType'
 found = playersAr[index].name.find(searchedPlayer);
 note: forward declaration of 'struct playerType'
 int lookUpPlayer(struct playerType playersAr[]); 
c++ arrays struct forward-declaration
1个回答
0
投票
问题是您已经在main函数中声明了结构。当您在主函数(即全局)之外定义函数时,如下所示:int lookUpPlayer(struct playerType playersAr[]);playerType类型不再有效。还要全局声明结构。

struct playerType { string playerName; string playerPosition; int playerTD; int playerCatches; int passingYds; int receivingYds; int rushingYds; };

© www.soinside.com 2019 - 2024. All rights reserved.