?
Solved

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

Posted on 2003-03-25
19
Medium Priority
?
797 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:mflores88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 5
  • +2
19 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8205750
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
 

Author Comment

by:mflores88
ID: 8205820
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208119
>>  #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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
sarda_ramesh earned 200 total points
ID: 8208465
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208472
>> 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
 
LVL 12

Expert Comment

by:Salte
ID: 8208982
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8209013
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8210561
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210618
>> if u do like this it wont worry

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

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

Mayank.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8211092
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
 

Author Comment

by:mflores88
ID: 8211352
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
 

Author Comment

by:mflores88
ID: 8211388
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8215345
>> 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
 
LVL 12

Expert Comment

by:Salte
ID: 8216452
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8216503
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
 
LVL 12

Expert Comment

by:Salte
ID: 8216587
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8217538
ya ::flush works ... while using iostream.h

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

regards
ramesh
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9558843
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 9559400
U R Right
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question