2012-03-22 68 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<<")"; 







}