c – Infile不完整类型错误

前端之家收集整理的这篇文章主要介绍了c – Infile不完整类型错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个以这种格式获取输入文件的程序:
  1. title author
  2.  
  3. title author
  4.  
  5. etc
  6.  
  7. and outputs to screen
  8.  
  9. title (author)
  10.  
  11. title (author)
  12.  
  13. etc

我目前遇到的问题是错误

“ifstream infile has incomplete type and cannot be defined”

以下是该计划:

  1. #include <iostream>
  2. #include <string>
  3. #include <ifstream>
  4. using namespace std;
  5.  
  6. string bookTitle [14];
  7. string bookAuthor [14];
  8. int loadData (string pathname);
  9. void showall (int counter);
  10.  
  11. int main ()
  12.  
  13. {
  14. int counter;
  15. string pathname;
  16.  
  17. cout<<"Input the name of the file to be accessed: ";
  18. cin>>pathname;
  19. loadData (pathname);
  20. showall (counter);
  21. }
  22.  
  23.  
  24. int loadData (string pathname) // Loads data from infile into arrays
  25. {
  26. ifstream infile;
  27. int counter = 0;
  28. infile.open(pathname); //Opens file from user input in main
  29. if( infile.fail() )
  30. {
  31. cout << "File Failed to open";
  32. return 0;
  33. }
  34.  
  35. while (!infile.eof())
  36. {
  37. infile >> bookTitle [14]; //takes input and puts into parallel arrays
  38. infile >> bookAuthor [14];
  39. counter++;
  40. }
  41.  
  42. infile.close;
  43. }
  44.  
  45. void showall (int counter) // shows input in title(author) format
  46. {
  47. cout<<bookTitle<<"("<<bookAuthor<<")";
  48. }

解决方法

文件流在标题< fstream>中定义.而且你不包括它.

你应该添加

  1. #include <fstream>

猜你在找的C&C++相关文章