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.
mflores88Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SalteCommented:
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
0
mflores88Author Commented:
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.
0
Mayank SAssociate Director - Product EngineeringCommented:
>>  #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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

sarda_rameshCommented:
Hi there,
  I have ur problem statement. This problem occurs if u r using "flush" within a member function of a class which is extending fstream because flush is a member function the base class.
  Instead of the statement

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

  try using

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

  It should work and I hope it will solve ur problem.

Regards
Ramesh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
SalteCommented:
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
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
sarda_rameshCommented:
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
0
Mayank SAssociate Director - Product EngineeringCommented:
>> if u do like this it wont worry

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

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

Mayank.
0
SalteCommented:
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
0
mflores88Author Commented:
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
0
mflores88Author Commented:
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!
0
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
SalteCommented:
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
0
sarda_rameshCommented:
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

0
SalteCommented:
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
0
sarda_rameshCommented:
ya ::flush works ... while using iostream.h

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

regards
ramesh
0
bcladdCommented:
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

0
sarda_rameshCommented:
U R Right
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.