【问题标题】:Passing ifstream to a function in C++将 ifstream 传递给 C++ 中的函数
【发布时间】:2010-02-23 02:09:49
【问题描述】:

我正在尝试为一个基因测序项目构建 16 种不同的后缀树。它们是在 main 中构建的

int main()
{  
  ifstream fp;  
  fp.open("filepath", ifstream::in);  
  Tree I(fp);  
  fp.close();

我正在尝试通过以下代码在构造函数中使用它们:

Tree::Tree(ifstream &fp)   
{  
  string current = "";  
  char* line;  
  fp.getLine(line, 100); //ignore first line  
  for(int i=0; i<100; i++)     
    {  
      char temp = (char)fp.get();  
      if(temp=='\n')i--;  
      else current+=temp;  
    }  
  insert(current);  
  while(fp.good())  
    {  
      current = current.substr(1,99);  
      char temp = (char)fp.get();  
      if(temp=='\n')temp=(char)fp.get();  
      if(temp==EOF) break;  
      current+=temp;  
      insert(current);  
    }  
}  

当我尝试编译时,我在使用 fp 的每个实例中都会收到这些错误:

suffix.cpp: 在构造函数Tree::Tree(std::ifstream&amp;):
suffix.cpp:12: 错误:无效使用未定义类型struct std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:89:错误:struct std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;的声明
suffix.cpp:15: 错误:无效使用未定义类型struct std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:89:错误:struct std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;的声明

【问题讨论】:

    标签: c++ ifstream


    【解决方案1】:

    您是否#included fstream 标头?

    【讨论】:

    • +1。 std::basic_ifstream 是在 &lt;iosfwd&gt; 中前向声明的,但 &lt;fstream&gt; 实际上并未包含在代码所在的源文件中。
    • 我将它包含在我的 main 中,但没有包含在具有违规功能的文件中。感谢您的帮助。
    【解决方案2】:

    您的源文件中似乎有#included 而不是

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多