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

发布于 2024-08-22 12:24:54 字数 1444 浏览 2 评论 0 原文

我正在尝试为基因测序项目构建 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&):
suffix.cpp:12: 错误:无效使用未定义类型 struct std::basic_ifstream; >
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:89: 错误: struct std 的声明::basic_ifstream; >
suffix.cpp:15: 错误:无效使用未定义类型 struct std::basic_ifstream; >
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:89: 错误: struct std 的声明::basic_ifstream; >

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

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> >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-08-29 12:24:55

看起来您已经#included ;而不是 在你的源文件中。

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

诗酒趁年少 2024-08-29 12:24:54

您是否 #include了 fstream 标头?

Have you #included the fstream header?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文