Reputation: 33
//i have two errors in my code
#include <iostream>
#include<iomanip>
#include<fstream>
using namespace std;
struct PLAYER
{
string first_name;
string last_name;
};
void showFile(fstream&, PLAYER&); // line 13
int main()
{
const int MAX=21;
PLAYER array[MAX];
ifstream inputFile;
inputFile.open("PlayerNames.txt",ios::in);
if(inputFile)
{
showFile(inputFile, array); // line 22
}else
cout<<"\n\nError opening file";
inputFile.close();
return 0;
}
void showFile(fstream &file, PLAYER &array )
{
int index=0;
int p_num=1;
string lname, fname;
file>>lname>>fname;
while(!file.eof())
{
// array[index].last_name=
cout<<lname<<" "<<fname;
//array[index].first_name=fname;
//array[index].player_number=p_num;
index++;
p_num++;
file>>lname>>fname;
}
// cout<<"\n"<<index;
//cout<<lname<<" "<<fname;
}
This program worked finally untill i put it in functions. I have two errors in this program line 22 error: invalid intialization of reference type std:: fstream line 13 error: in passing argument 1 of void showFile(std:: fstream&, PLAYER&)
Upvotes: 1
Views: 187
Reputation: 24163
An ifstream
can't be converted to an fstream
, only an istream
.
In the documentation you can see basic_ifstream
is derived from basic_istream
, not basic_fstream
.
Make your function:
void showFile(istream&, PLAYER&);
This is actually better in many ways. For one it's correct (c: But also it means you can test it with any input stream rather than just a file stream. It is more loosely coupled and programs to the more abstract interface.
Upvotes: 1
Reputation: 57749
You function declaration at line 13 shows you are passing 1 PLAYER
object, not an array. If you want to stay with arrays, search StackOverflow for "[C++] pass array function".
I highly recommend using std::vector
as it has easier syntax when passing to functions.
Upvotes: 1