Jump to content
  • Advertisement
Sign in to follow this  
Samsonite

variable std::ifstream file' has initializer but incomplete type

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


bool CGameGame::GetStory(string filename)
{
     //open up the file for reading
     ifstream file (filename, ios::in);
     //if the file is not open
     if(!file.is_open())
     {
                        cout << "Could not open file " << filename << "!"<<endl;
                        //return failure
                        return false;
     }
     //"Mark" the point in file
     l = file.tellg();
     //Search for the end
     file.seekg(0,ios::end);
     //"Mark" the end point
     m = file.tellg();
     //Search for the beginning
     file.seekg(0,ios::beg);
     //Allocate a char buffer with the size of the file
     filebuffer = new char[m];
     //While the end of file is not reached
     while(!file.eof())
     {
                       //Read each line into the buffer called "filebuffer"
                       file.getline(filebuffer, 100);
     } 
     //Then return true;
     return true;
}



This little piece of code returns
54 C:\Dev-Cpp\Data-drive gamesz\AGClass.h variable `std::ifstream file' has initializer but incomplete type 



So what am I doing wrong? Help is much appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Have you included the fstream header file?



#include<fstream>

int main()
{
std::ifstream myfile("filename.txt");
}





You also should not need to specify the std::ios::in flag as a second parameter for the ifstream constructor, as it is implicit for an ifstream, provided through a default constructor argument.


explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);




There is also a cool trick for reading a file into a string, which avoids the loops and explicit memory allocation, it goes something like:


#include<fstream>
#include<sstream>
#include<string>

std::string GetStory()
{
std::ifstream file("filename.txt");
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}

Share this post


Link to post
Share on other sites
Use ifstream file (filename.c_str(), ios::in); instead. And you don't need to specify ios::in, by the way.
The reason you need to specify .c_str() is that you are passing in a std::string object, and the constructor expects a char *. std::string does not have implicit char * conversion, so you must use the c_str() method to "convert" to a char *.


jfl.

Share this post


Link to post
Share on other sites
Well, I have
#include <iostream>
, so I thought that since ifstream is a part of iostream, then i wouldn't ned to in clude ifstream.

Share this post


Link to post
Share on other sites
@jfl:

Now I can see the problem. But even though I changed it, it didn't work.

So now my code looks like this:


bool CGameGame::GetStory(string filename)
{
//open up the file for reading
ifstream file (filename.c_str(), ios::in);
//if the file is not open
if(!file.is_open())
{
cout << "Could not open file " << filename << "!"<<endl;
//return failure
return false;
}
//"Mark" the point in file
l = file.tellg();
//Search for the end
file.seekg(0,ios::end);
//"Mark" the end point
m = file.tellg();
//Search for the beginning
file.seekg(0,ios::beg);
//Allocate a char buffer with the size of the file
filebuffer = new char[m];
//While the end of file is not reached
while(!file.eof())
{
//Read each line into the buffer called "filebuffer"
file.getline(filebuffer, 100);
}
//Then return true;
return true;
}

Share this post


Link to post
Share on other sites
You do need to #include <fstream>, as Mxz has pointed out.

[edit] By the way (this has nothing to do with your problem), you may want to use std::string instead of char * for filebuffer.


jfl.

Share this post


Link to post
Share on other sites
The iostream header file only declares the following objects



namespace std {
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}




Some implementations also include the <ostream> header and the <istream> header, but they are not required to by the C++ standard.

The ifstream object is declared in the <fstream> header as


typedef basic_ifstream<char> ifstream;



So you must include that header.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!