开发者

Passing ifstream to a function in C++

I'm trying to build 16 different suffix trees for a gene sequencing project. They're being built in main as such

int main()
{  
  ifstream fp;  
  fp.open("filepath", ifstream::in);  
  Tree I(fp);  
  fp.close();
开发者_C百科

I'm attempting to use them in my constructor with this code:

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

When I attempt to compile, I get these errors for every instance in which I use fp:

suffix.cpp: In constructor Tree::Tree(std::ifstream&):

suffix.cpp:12: error: invalid use of undefined type 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: declaration of struct std::basic_ifstream<char, std::char_traits<char> >

suffix.cpp:15: error: invalid use of undefined type 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: declaration of struct std::basic_ifstream<char, std::char_traits<char> >


Have you #included the fstream header?


It looks like you have #included <iosfwd> instead of <fstream> in your source files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜