Infile不完整类型错误

kd7*_*vdb 6 c++ arrays file-io function

我正在构建一个以这种格式获取输入文件的程序:

title author

title author

etc

and outputs to screen 

title (author)

title (author)

etc
Run Code Online (Sandbox Code Playgroud)

我目前遇到的问题是错误:

"ifstream infile的类型不完整,无法定义"

以下是该计划:

#include <iostream>              
#include <string>
#include <ifstream>
using namespace std; 

string bookTitle [14];
string bookAuthor [14];
int loadData (string pathname);         
void showall (int counter);

int main ()

{
int counter;  
string pathname;

cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
}


int loadData (string pathname) // Loads data from infile into arrays
{
    ifstream infile; 
    int counter = 0;
    infile.open(pathname); //Opens file from user input in main
    if( infile.fail() )
     {
         cout << "File failed to open";
         return 0;
     }   

     while (!infile.eof())
     {
           infile >> bookTitle [14];  //takes input and puts into parallel arrays
           infile >> bookAuthor [14];
           counter++;
     }

     infile.close;
}

void showall (int counter)        // shows input in title(author) format
{
     cout<<bookTitle<<"("<<bookAuthor<<")";
}
Run Code Online (Sandbox Code Playgroud)

Alo*_*ave 17

文件流在标题中定义,<fstream>您不包括它.

你应该添加:

#include <fstream>
Run Code Online (Sandbox Code Playgroud)