[我试图为棒球运动员提供一个结构,并创建了一个包含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[]);
int lookUpPlayer(struct playerType playersAr[]);
,playerType
类型不再有效。还要全局声明结构。struct playerType
{
string playerName;
string playerPosition;
int playerTD;
int playerCatches;
int passingYds;
int receivingYds;
int rushingYds;
};