将ifstream传递给C++中的函数

Jes*_*lch 3 c++ ifstream

我正在尝试为基因测序项目构建16种不同的后缀树.他们正在建造这样的主体

int main()
{  
  ifstream fp;  
  fp.open("filepath", ifstream::in);  
  Tree I(fp);  
  fp.close();
Run Code Online (Sandbox Code Playgroud)

我试图在我的构造函数中使用它们与此代码:

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);  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

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

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

sth*_*sth 6

你有#included fstream标题吗?


Bri*_*ews 5

看起来你的源文件中有#included <iosfwd>而不是<fstream>.