【问题标题】:Infile incomplete type errorInfile 不完整类型错误
【发布时间】:2012-03-22 05:24:27
【问题描述】:

我正在构建一个程序,该程序采用这种格式的输入文件:

title author

title author

etc

and outputs to screen 

title (author)

title (author)

etc

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

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

【问题讨论】:

标签: c++ arrays function file-io


【解决方案1】:

文件流在标头&lt;fstream&gt; 中定义,您没有包含它。

你应该添加:

#include <fstream>

【讨论】:

  • 我得到的新错误是: no matching function for call to `std::basic_ifstream >::open(std::string&)'
【解决方案2】:

这是我的代码,之前的错误已修复现在我在输入文本文件的名称后遇到程序崩溃的问题。

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







}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2018-04-20
    相关资源
    最近更新 更多