Discussion:
ofstream << TDateTime
(too old to reply)
Alexander Aliev
2008-05-08 20:44:55 UTC
Permalink
Hi,

I get Stack Overflow error on the following statement:
...
Log << Now();
...

There are following definitions:

class logstream : public ofstream {...}; // no redefinition for <<
...
logstream Log;

When debugging I enter in sysclass.h :

// TDateTime IOStream operators
//
inline std::ostream& operator << (std::ostream& os, const TDateTime& arg)
{
os << arg;
return os;
}

If I understand correctly then on statement os << arg program should
recursively call the same function and get stack overflow.
I use RAD2007 trial, but in BCB5 in this place I see

os << AnsiString(arg);

I suspected this is a bug, but when I try to repeat the situation
in small test program compiler gives error 'ambiguity between
... operator << (int) and ... operator << (double)' at that very line

Log << Now();

Could you suggest what I am doing wrong?

Thank you for any hint,
Akexander
Leo Siefert
2008-05-09 10:56:00 UTC
Permalink
Post by Alexander Aliev
...
Log << Now();
...
I suspected this is a bug, but when I try to repeat the situation
in small test program compiler gives error 'ambiguity between
... operator << (int) and ... operator << (double)' at that very line
It certainly looks like a bug to me, and it worked up to BCB6. I
suspect that the C++ headers are being incorrectly generated by the
Delphi compiler.

I can reproduce the error in a minimal program:

#include <iostream>
#include <Sysutils.hpp>

int main()
{
std::cout << Now();
return 0;
}
Post by Alexander Aliev
Could you suggest what I am doing wrong?
Nothing.

You can work around this issue by changing your code to:

Log << AnsiString( Now() );

Please enter a QC report for this.

- Leo
Fraser Ross
2008-05-09 14:39:36 UTC
Permalink
The Now function doesn't appear to be known with BDS2006 even when
SysUtils.hpp is included.

Fraser.
Nigel Jones
2008-05-09 16:09:43 UTC
Permalink
Post by Fraser Ross
The Now function doesn't appear to be known with BDS2006 even when
SysUtils.hpp is included.
Use TDateTime().CurrentDateTime() ?


Nigel
Bob Gonder
2008-05-09 16:13:42 UTC
Permalink
Post by Fraser Ross
The Now function doesn't appear to be known with BDS2006 even when
SysUtils.hpp is included.
What is the exact error?

There are at least 4 different Now functions.

Try SysUtils::Now()
Alexander Aliev
2008-05-09 21:25:14 UTC
Permalink
Post by Bob Gonder
There are at least 4 different Now functions.
Try SysUtils::Now()
This is the only Now() function without parameters
in RAD2007.
Alexander Aliev
2008-05-09 21:22:25 UTC
Permalink
Post by Leo Siefert
Log << AnsiString( Now() );
Leo, this does not help in my case: instead of compiler error
'ambiguity between ... operator << (int) and ... operator << (double)'
now it says
'ambiguity between ... AnsiString::AnsiString(const AnsiString&)
and ... AnsiString::AnsiString(int)'
Post by Leo Siefert
Please enter a QC report for this.
I still doubt: can't understand why in one project this is compiled
but in another project - not. Definitely there is a reason...

Regards,
Alexander
Bob Gonder
2008-05-10 02:29:00 UTC
Permalink
Post by Alexander Aliev
Post by Leo Siefert
Log << AnsiString( Now() );
Leo, this does not help in my case: instead of compiler error
'ambiguity between ... operator << (int) and ... operator << (double)'
now it says
'ambiguity between ... AnsiString::AnsiString(const AnsiString&)
and ... AnsiString::AnsiString(int)'
Log << DateTimeToStr( Now() );
Post by Alexander Aliev
I still doubt: can't understand why in one project this is compiled
but in another project - not. Definitely there is a reason...
BCB5 Help:(also under TDateTime Class in 2006 help)
Note: If you #include <iostream> or #define VCL_IOSTREAM before the include statement
for systdate.h (which is typically included indirectly via vcl.h), you can use the
streaming operators (<< and >>) with TDateTime values as an argument:

I suppose is systdate.h got included before VCL, it would turn off this functionality,
which seems to have happened in your case.

Then again, might have been a minor difference between RW and Dinkum interface with VCL.
Or, it just plain broke (regressed?) in 2007 (hope not).
Fraser Ross
2008-05-10 12:01:58 UTC
Permalink
This member function performs the same task:
Now().DateTimeString();


With BDS2006 Now() generates an unresolved external linking error when
runtime packages are off.

Fraser.
Andriy R
2008-05-12 15:57:50 UTC
Permalink
Post by Alexander Aliev
Hi,
...
Log << Now();
...
class logstream : public ofstream {...}; // no redefinition for <<
...
logstream Log;
// TDateTime IOStream operators
//
inline std::ostream& operator << (std::ostream& os, const TDateTime& arg)
{
os << arg;
return os;
}
If I understand correctly then on statement os << arg program should
recursively call the same function and get stack overflow.
I use RAD2007 trial, but in BCB5 in this place I see
os << AnsiString(arg);
I suspected this is a bug, but when I try to repeat the situation
in small test program compiler gives error 'ambiguity between
... operator << (int) and ... operator << (double)' at that very line
Log << Now();
Could you suggest what I am doing wrong?
Thank you for any hint,
Akexander
I had the similar problem when switched from BCB6 to BCB2007.
My problem was in using #define VCL_IOSTREAM in BCB6 project.

Regarding BCB2007 help this posibility is deprecated.
your code should looks like
Log << Now().DateTimeString().c_str();

next is extraction from BCB6 help:
Note: To utilize the C++ streaming operators (<< and >>) with AnsiString, you must use #include <iostream> or #define VCL_IOSTREAM before #include <dstring.h>. This occurs automatically if you include vcl.h or system.h and defines the operators as follows:

ostream& operator << (ostream& os, const AnsiString& arg);
istream& operator >> (istream& is, AnsiString& arg);
Alexander Aliev
2008-05-12 19:11:55 UTC
Permalink
Post by Andriy R
I had the similar problem when switched from BCB6 to BCB2007.
My problem was in using #define VCL_IOSTREAM in BCB6 project.
Regarding BCB2007 help this posibility is deprecated.
I think no. It does not mention in help but there is a comment in sysclass.h
about meaning VCL_IOSTREAM.
Post by Andriy R
your code should looks like
Log << Now().DateTimeString().c_str();
It is enough
Log << Now().DateTimeString();

As for error Stack overflow while output Now() I posted it to QC (61989).

Regards,
Alexander
Leo Siefert
2008-05-13 12:14:38 UTC
Permalink
Post by Alexander Aliev
I posted it to QC (61989).
Report No: 61989 Status: Open
Stack overflow on output Now() to ofstream
http://qc.codegear.com/wc/qcmain.aspx?d=61989
QCWIN:Defect_No=61989

- Leo
Andriy R
2008-05-13 13:18:28 UTC
Permalink
Post by Leo Siefert
Post by Alexander Aliev
I posted it to QC (61989).
Report No: 61989 Status: Open
Stack overflow on output Now() to ofstream
http://qc.codegear.com/wc/qcmain.aspx?d=61989
QCWIN:Defect_No=61989
- Leo
See previously posted report
http://qc.codegear.com/wc/qcmain.aspx?d=59764

Comment from QA
Tomohiro Takahashi at 3/21/2008 1:25:53 AM - [Reply to this comment]
I think VCL_IOSTREAM is obsolete. So, please try to use 'os << DateTimeToStr(Now()).c_str();' instead of using VCL_IOSTREAM.
Leo Siefert
2008-05-13 19:19:40 UTC
Permalink
Post by Andriy R
http://qc.codegear.com/wc/qcmain.aspx?d=59764
Hmm. Not sure why my search didn't turn that one up. But the code
sample in your report did not rely on the possibly deprecated macro
from that report, so I still consider it a bug. That report was closed
because it had no clear steps and no sample code, but yours includes
good demo code and clear steps. Hard to see how anyone cold consider
the code from the VCL headers not buggy.

- Leo

Loading...