Link to home
Start Free TrialLog in
Avatar of mflores88
mflores88

asked on

cout << flush not working in my class that derives from fstream

Normally when I prompt the user for input I flush the buffer, and it works fine:
     cout << "Enter path to input file: " << flush;

But now I'm trying to do the same thing in a class_name.cpp file and I get:
     error C2679: binary '<<' : no operator defined which takes a right-hand
     operand of type '' (or there is no acceptable conversion)

In the class_name.h file I do this to inherit from fstream:
     #include <fstream.h>
     class class_name:public fstream
     {
        ...
     }

If I remove the flush keyword it works fine. And, kinda weird, but the endl keyword also works fine.

I do have this in class_name.cpp:
     #include "class_name.h"
     #include <iostream.h>

Why does flush bomb out?

Thanks.
Avatar of Salte
Salte

sounds weird, flush is defined for ostream and fstream inherit from ostream....

Could you tell us exactly what the error message is? It appears that only the first part of the error message is included in your text.

The error message says 'no operator defined which takes a right hand operator of type " (or there is no acceptable conversion)....

I was sort of expecting to see a typename between the word 'type' and the parenthesis.

Also, what version of iostream are you using? Do you include <fstream> or do you include <fstream.h>. That is also very important to know.

Alf
Avatar of mflores88

ASKER

No, there is no type between the single tic's. Just empty. The error is reported just how I copied/pasted it.

My original post shows exactly what I'm doing:
     in the .h file:  #include <fstream.h>

     in the .cpp file: #include "class_name.h"
                       #include <iostream.h>

Something else strange is that this also works:
     #ifdef DEBUG
     cout << "\nDestructing File...\n" << flush;
     #endif

So the preprocessor works, I guess.

Thanks.
Avatar of Mayank S
>>  #include <fstream.h>

He's writing <fstream.h>, Alf. I guess we also need to see the statement where he's used flush with the derived class/ object.

Please post it, mflores88. I just tried a similar thing on my system - it works fine. I'm using Turbo C++ 3.0 on Windows 2000.

Mayank.
ASKER CERTIFIED SOLUTION
Avatar of sarda_ramesh
sarda_ramesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> try using

>> cout << "Enter path to input file: " ;
>> cout.flush();

cout << "Enter path to input file: " << flush ;

works too. And he already said that it is working fine on his system. The problem is while uing it with the derived class object.

Mayank.
Hmm.... Not sure what is going on, it ought to work.

Could it be that there is a bug? fstream.h and iostream.h are considered obsolete today and you might want to consider changing to fstream and iostream:

#include <fstream> instead of #include <fstream.h>

and

#include <iostream> instead of #include <iostream.h>

Also, it might be a good idea to include iostream before you include your own class. The file that defines your own class does include <fstream.h> though and it ought to work but this might be a cause of the problem through some obscure bug in the old and outdated iostream.h library.

Be aware that 'fstream' in the new library is not a class but a typedef. This should not give you any troubles though, you can still use it as baseclass.

Alf
That is why I asked him to post the statement in which the derived class/ object is using flush. He didn't post that part//

Mayank.
Hi Manyank ,
The person is calling the statement

 cout << "Enter path to input file: " << flush;

within a class that is extending the fstream class
if u do like this it wont worry
u can try it urself

regards
ramesh
>> if u do like this it wont worry

Was that supposed to mean 'it won't work'??

The 'cout << "...." << flush ;' definitely works.

Mayank.
Mayank,

He possibly refer to the fact that 'flush' inside a class that derives from fstream might refer to the method flush and not the global function flush that is used in the << flush. However, the compiler should be able to resolve that based on the different types of the two thingies named 'flush'. There is a

ostream & operator << (ostream & os, ostream & (* f)(ostream & os));

operator but as far as I know there's no

ostream & operator << (ostream & os, ostream & (ostream::* f)());

operator and so there shouldn't be any conflict.

Alf
Great ideas everybody. I tried all of this however, and it still doesn't work. Must be a bug on my system.

I guess I could just drop the flush altogether. Any danger in just not using it?

thanks
Actually, I take that back. This did work:
    cout.flush();

I guess I should get used to using this syntax instead of:
   cout << flush;

Thanks all!
>> cout.flush();

It will work all-right, but weren't we talking about dervied classes too? If its working inside a class derived from fstream, well, then the matter is solved.

Mayank.
well, cout.flush() will of course flush the cout stream and not the stream you're inside.

fstream::flush();

will work on the stream you're inside.

Alf
Mayank , sorry for that typing error.


Hi mflores88

the code

       cout.flush()

works within a class derived from fstream but the code

       cout<<flush

doesn't work within a class derived from fstream..

I am using TurboC3 and when i try using cout<<flush within a class extending fstream it gives me an error like ..

"Member function must be called or its address taken"

That means that it is unable to resolve the symbol "flush" and the compiler in this case is assuming the member function flush() and therefore cout<<flush; is not working.
and cout.flush() is working. I hope this solves the problem and makes it clear.

regards
ramesh

Hmm...it is possible it doesn't take the type into account when resolving for the name 'flush' It see there is a member named 'flush' and so it will not consider the global flush

If you change flush to std::flush or some such it would work though:

cout << std::flush;

should work even inside a class.

if you use old <iostream.h> you can use ::flush instead of std::flush.

Alf
ya ::flush works ... while using iostream.h

but using cout.flush() is a better programming style i feel

regards
ramesh
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to sarda_ramesh

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer

U R Right