What do I need to make this work?
#include <iostream>
#include <fstream>
using namespace std;
struct LoggedCout {
ofstream file;
LoggedCout(const char* name) : file(name, ios::app) {}
template<class T>
LoggedCout& operator<<(const T& t)
{ cout << t; file << t; return *this; }
};
LoggedCout lout("flushtest.txt");
int main() {
lout << "output";
lout << flush;
// no match for 'operator<<' in 'lout << std::flush'
{ char c; cin >> c; }
return 0;
}
Martin
--
Quidquid latine scriptum sit, altum viditur.
|
|
0
|
|
|
|
Reply
|
martin.eisenberg (676)
|
6/14/2006 4:52:47 PM |
|
Martin Eisenberg wrote:
> What do I need to make this work?
>
> #include <iostream>
> #include <fstream>
>
> using namespace std;
>
> struct LoggedCout {
> ofstream file;
> LoggedCout(const char* name) : file(name, ios::app) {}
> template<class T>
> LoggedCout& operator<<(const T& t)
> { cout << t; file << t; return *this; }
> };
>
> LoggedCout lout("flushtest.txt");
>
> int main() {
> lout << "output";
> lout << flush;
> // no match for 'operator<<' in 'lout << std::flush'
> { char c; cin >> c; }
> return 0;
> }
Was it you who asked a similar question about 'std::endl' yesterday?
If it was, why do you think it's different with 'flush'? It's just
another function pointer when used liek that in an expression.
If it wasn't, look in the archives for yesterday's postings here.
Please don't use this newsgroup as a write-only medium. Read it
before posting. Use Google Groups to read it beyond what your ISP's
news server retains.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|
|
0
|
|
|
|
Reply
|
v.Abazarov (13255)
|
6/14/2006 6:07:44 PM
|
|
Martin Eisenberg wrote:
> What do I need to make this work?
>
> #include <iostream>
> #include <fstream>
>
> using namespace std;
>
> struct LoggedCout {
> ofstream file;
> LoggedCout(const char* name) : file(name, ios::app) {}
> template<class T>
> LoggedCout& operator<<(const T& t)
> { cout << t; file << t; return *this; }
> };
Add this to your class:
typedef ostream&(*IOManip)(ostream&);
LoggedCout& operator<<( const IOManip iomanip )
{
cout << iomanip;
file << iomanip;
return *this;
}
Or better, consider making it a non-member function since the whole
thing is public anyway.
>
> LoggedCout lout("flushtest.txt");
>
> int main() {
> lout << "output";
> lout << flush;
> // no match for 'operator<<' in 'lout << std::flush'
> { char c; cin >> c; }
> return 0;
> }
[...]
> --
> Quidquid latine scriptum sit, altum viditur.
Sic, sed nemo id intelleget.
Cheers! --M
|
|
0
|
|
|
|
Reply
|
mlimber (2146)
|
6/14/2006 6:23:41 PM
|
|
mlimber wrote:
> Martin Eisenberg wrote:
>> struct LoggedCout {
>> ofstream file;
>> LoggedCout(const char* name) : file(name, ios::app) {}
>> template<class T>
>> LoggedCout& operator<<(const T& t)
>> { cout << t; file << t; return *this; }
>> };
>
> Add this to your class:
>
> typedef ostream&(*IOManip)(ostream&);
> LoggedCout& operator<<( const IOManip iomanip )
> {
> cout << iomanip;
> file << iomanip;
> return *this;
> }
Thanks! You're right, Victor, I should have seen that endl thread.
Let me change the question: why is T in my code not deduced as
something like IOManip above? Shouldn't it work as per 14.8.2/3?
Martin
--
In the Wacky Protestor's return, he hijacks the popular
children's TV show, "Mr. Funky's Wild Time", and uses it
to control the hearts and minds of every child watching.
-- http://www.bibleman.com/bibleman/store.jsp
|
|
0
|
|
|
|
Reply
|
martin.eisenberg (676)
|
6/14/2006 6:57:35 PM
|
|
Martin Eisenberg wrote:
> [..]
> Let me change the question: why is T in my code not deduced as
> something like IOManip above? Shouldn't it work as per 14.8.2/3?
You supply a pointer to function. What would you expect T to be?
Have you tried changing the argument to 'T' instead of 'const T&'?
Notice that 'IOManip' is not passed by reference.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|
|
0
|
|
|
|
Reply
|
v.Abazarov (13255)
|
6/14/2006 7:30:06 PM
|
|
Victor Bazarov wrote:
> Martin Eisenberg wrote:
>> Let me change the question: why is T in my code not deduced as
>> something like IOManip above? Shouldn't it work as per 14.8.2/3?
>
> You supply a pointer to function. What would you expect T to be?
Actually, I just realized that "flush" does not name a single entity
so there's no way to deduce anything at all in the context in
question. Right?
Martin
--
Sphinx of black quartz, judge my vow!
--David Lemon
|
|
0
|
|
|
|
Reply
|
martin.eisenberg (676)
|
6/14/2006 8:31:24 PM
|
|
Martin Eisenberg wrote:
> Victor Bazarov wrote:
>> Martin Eisenberg wrote:
>
>>> Let me change the question: why is T in my code not deduced as
>>> something like IOManip above? Shouldn't it work as per 14.8.2/3?
>>
>> You supply a pointer to function. What would you expect T to be?
>
> Actually, I just realized that "flush" does not name a single entity
> so there's no way to deduce anything at all in the context in
> question. Right?
Probably.
'std::flush' is a template. 'flush' also is an 'std::basic_ostream'
member function. 'std::basic_ostream' declares at least three overloaded
operator << functions that take function pointers. As soon as you write
'flush' there, the compiler tries to understand which 'flush' you mean.
Most likely it finds the 'std::flush' template and tries to see if it
can figure out that template's arguments. In fact, it has to try the
same template arguments as the 'basic_ostream' has. It succeeds, AFAICT.
I am not sure my explanation makes sense. Josuttis probably has a better
one in his Standard Library book.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|
|
0
|
|
|
|
Reply
|
v.Abazarov (13255)
|
6/14/2006 9:43:07 PM
|
|
|
6 Replies
28 Views
(page loaded in 0.096 seconds)
Similiar Articles: vgchgid failed on hp-ux - comp.sys.hp.hpuxaccept() and getpeername() return 0.0.0.0 - comp.sys.hp.hpux ... vgchgid failed on ... Older versions of HP-UX: install the LVM cumulative ... manipulation changes LVM ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Some text processing questions - comp.lang.vhdlFor the meantime I made them accept strings and read the line into a string. ... not standard, most simulators probably provide some form of C-like string manipulation ... Problems getting photos from Canon EOS D20 - comp.graphics.apps ...Accept the facts, it won't hurt. > > Now that the authors of PL32 have a 30D ... (In just this one > little area of image manipulation capabilities no less. EMSDIST in TACL script. - comp.sys.tandemI imagine it would accept longer records, but whether it uses the full record ... it can be put into an edit file and uploaded to a Windows, Unix or Mac for manipulation. Randperm, Randi, and Shuffle - comp.soft-sys.matlab... of creating Y=ONES(1,M,'uint8') at first, draw a number X, if Y(X) is 1, accept X ... Fancy index manipulation can be very powerful but it tends to obscure the code, for ... Throttling network traffic - comp.arch.embedded... net-neighbors) When listening for TCP connections, just delay accepting the ... and policy routers [ tc - traffic classification ] # do further packet manipulation ... Smallest circle covering set of points - comp.graphics.algorithms ...We can make it exactly into an SDP problem with a little manipulation. We consider a ... Let f_j : R^n -> R: f_j(x) = ||x - p_j||^2 f = max {f_j} If you accept that ... Need a FORTRAN compiler for Win7 (or XP) - comp.lang.fortran ...I have no idea if they will accept old ftn. It mainly depends on how old - for ... Bit manipulation. 7. Equivalence and Common that depend on architecture. Specific order to install Access 2003 and 2007 (2010 for that ...... 2003\ C:\Program Files\Microsoft Office 2007\ I'm saying that if you accept ... Bit manipulation. 7. ... says on down that's a big order ... register for support when ... 5 Counterfeits of Love (and why people accept them) Part V: Abuse ...LifeTeen.com Blog 5 Counterfeits of Love (and why people accept them) Part V: Abuse and Manipulation Dealing With Manipulative People - The Ross Institute Internet ...... reform, coercive persuasion, influence, manipulation ... to believe what our gut tells us about our manipulator's character. Recognizing Aggressive Agendas. Accepting how ... 7/24/2012 2:16:07 AM
|