2012-03-22 64 views
6

我建立一个程序,需要输入文件格式如下:INFILE不完全类型错误

title author 

title author 

etc 

and outputs to screen 

title (author) 

title (author) 

etc 

我目前得到的问题是一个错误:

"ifstream infile has incomplete type and cannot be defined"

以下是节目:

#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<<")"; 
} 
+1

可能重复http://stackoverflow.com/questions/1057287/offstream-error-in-c – Vaibhav 2012-03-22 05:36:56

+0

有没有这样的标准包括文件作为''。你的编译器应该显示一个错误。如果没有,请检查其选项。在这种情况下,您*确实希望发生错误。 – atzz 2012-03-22 08:21:53

回答

13

文件流在报头<fstream>中定义,您不包括它。

您应该添加:

#include <fstream> 
+0

我得到的新错误是: 没有匹配函数调用'std :: basic_ifstream > :: open(std :: string&)' – kd7vdb 2012-03-22 13:49:05

0

这里是我的代码以固定前面的错误现在我拿到程序后,我崩溃输入文本文件的名称的问题。

#include <iostream>    
#include <string> 
#include <fstream> 
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 
{ 
    fstream infile; 
    int counter = 0; 
    infile.open(pathname.c_str()); //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<<")"; 







}