fstream problems on g++ compiler

VBStudent
VBStudent used Ask the Experts™
on
I am trying to overload the << to be used with the fstream in a class definition. The function works fine on Visual C++, but doesn't work at all when trying to compile the same code on linux g++.

This is what I have:

#include <fstream>
using namespace std;

class Hashtable
{
public:
     Hashtable(int=11);
     Hashtable & operator<<(fstream&);
     void operator>>(fstream&);
etc..........}

 
void Hashtable::operator >>(fstream &theFile)
{
     Employee emp;
     char fileName[16];
     int count = 0;
     cout << "Enter the name of a file: ";
     cin >> fileName;
theFile.open(fileName,ios::binary|ios::in|ios::out|ios::app);

     
The file will not open at all when compiled in g++ but works fine in Visual C++. I've been told that the problem is with the g++ compiler and that I should use fstream.h without the "using" keyword. I've tried that, but with no success. On the same hand, fstream works fine on the same compiler when I'm not trying to overload the << or >> in a class definition. Any suggestions would be greatly appreciated. Thank You
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
EOL

Commented:
I havn't tried, but you can try to use

 void operator>>(std::fstream&);
or

using std::fstream

Also to write a friend operator isn't an all that bad idea anyway.
this is done like.

                    #include <fstream>
                    using std::fstream;

                    class Haschtable;
                    Haschtable& operator >>( Haschtable& hash, fstream &theFile );

                    class Hashtable
                    {
                    public:
                        Hashtable(int=11);
                        friend Haschtable& operator>>(Haschtable&, fstream&);
                    }

                    Haschtable& operator >>( Haschtable& hash, fstream &theFile )
                    {
                        return hash;
                   }

Commented:
How do you spell Hash EOL? Eek.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Commented:
How do you spell Hash EOL? Eek.

Commented:
I do not know if that will solve your problem but stream operators are supposed to be global friends:

    std::istream& operator>>(std::istream &in, Hashtable &hash)
    {
        // ...
        return in;
    }

    std::ostream& operator<<(std::istream &out, Hashtable &hash)
    {
        // ...
        return out;
    }

A way to do it without friends is listed below.


#include <iostream>
#include <fstream>
using namespace std;

// header/prototypes /////////////////////

class Hashtable
{
      public:
            Hashtable(int=11);
            Hashtable & operator<<(fstream&);
            void operator>>(fstream&);

            void ostream_insert(ostream& sout) const;

      // and so on...
};

ostream& operator<<(ostream& out, const Hashtable& h);



// implementation ////////////////////////

void Hashtable::ostream_insert(ostream& sout) const
{
      Employee emp;
      sout << emp.something;
      sout << emp.something_else;
      // and so on
}


ostream& operator<<(ostream& out, const Hashtable& h)
{
      h.ostream_insert(out);
      return out;
}


// the client code //////////////////////

Hashtable myhash;
// do stuff to myhash

ofstream fout("output.txt");

fout << myhash << endl;


Essentially you define public functions to do your stream insertion (and extraction if need be), then define the operators as auxillary functions outside of the class.

The reason why you can't define a stream operator inside the class is because the client code will use syntax like:

myhash << fout;

instead of the typical

fout << myhash;


If you overload an binary operator as a member of the class, the instance of the class becomes the first operand and the parameter of the overloaded operator will be the second operand.  In your case:

Hashtable & operator<<(fstream&);

means that code calling that function will look like:

myhash << fin

which is sorta backwards in C++.

Defining the binary operator outside allows you to specify the ordering.  In my prototype above:

ostream& operator<<(ostream& out, const Hashtable& h)

first operand is the stream, second operand is the hashtable, so the client code then can call:

fout << myhash;


Hope this helps -- i have a complete example if need be.
EOL

Commented:
ups, see me blusching, jo my goschn fridn. You know if you'r in a hurry it's sometimes hard as a foreign writer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial