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
suffix.cpp:12: error: invalid use of undefined typeTree::Tree(std::ifstream&)
: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 ofstruct std::basic_ifstream<char, std::char_traits<char> >
suffix.cpp:15: error: invalid use of undefined typestruct 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 ofstruct 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.
精彩评论