Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

emileej

Cannot pass a ifstream to a function

This topic is 5291 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

This is my function:
char ReadFormatted(ifstream ifs){
    //some ifs.read(); stuff

}
I am, as the include paths will show, using DevC++ and the standard compiler that comes with it. This is the compiler output:
C:/Programmer/Dev-Cpp/include/c++/bits/ios_base.h: In copy constructor 
   `std::basic_ios >::basic_ios(const 
   std::basic_ios >&)'':
C:/Programmer/Dev-Cpp/include/c++/bits/ios_base.h:421: `
   std::ios_base::ios_base(const std::ios_base&)'' is private
CPage.cpp:349: within this context

C:/Programmer/Dev-Cpp/include/c++/streambuf: In copy constructor 
   `std::basic_filebuf >::basic_filebuf(const 

   std::basic_filebuf >&)'':
C:/Programmer/Dev-Cpp/include/c++/streambuf:486: `std::basic_streambuf<_CharT, 
   _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) 
   [with _CharT = char, _Traits = std::char_traits]'' is private
CPage.cpp:349: within this context 
Is there no way to have a function read from an ifstream defined elsewhere? I am thinking that perhaps some part of the ifstream object can be used to refer to it, but I am not all that experienced with fstream.

Share this post


Link to post
Share on other sites
Advertisement
Yea. Thanks. Ofcourse I need to be working with a reference. *bashes self on forehead*. Thanks fallen :D

Share this post


Link to post
Share on other sites
Hmm. Didnt get me very far :/
As soon as I use the ifstream - the compiler complains:

CReader.cpp: In function `int ReadInt(const std::ifstream&)'':
CReader.cpp:10: passing `const std::ifstream'' as `this'' argument of `
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::get(_CharT&) [with _CharT = char, _Traits =
std::char_traits]'' discards qualifiers


I simply do this:
char r;
ifs.get(r);

Share this post


Link to post
Share on other sites
Remove the const qualifier on the ifstream, you can''t call non-const functions on a const reference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Haha, I just made that same mistake on my program.

Share this post


Link to post
Share on other sites

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