Click to See Complete Forum and Search --> : _int64 : cin and cout


Guysl
April 25th, 2004, 06:33 AM
how do I use cin/cout with _int64 type?

Guysl
April 25th, 2004, 10:14 AM
does the following overload make sense?

ostream& operator<<(ostream& out, const unsigned _int64& Int64)
{
char strVal[34];
_ui64toa (Int64,strVal,10);
out << strVal;
return out;
}

jlou
April 26th, 2004, 01:02 PM
cin and cout recognize __int64 for me just fine:#include <iostream>
int main()
{
__int64 myInt = 9223372036854775807;
std::cout << "Big number: " << myInt << std::endl;
}That works fine in MSVC++ 6.0. If you are using the old, non-standard header (<iostream.h>) then it will give you an error. Just switch to the new headers.

Guysl
April 26th, 2004, 01:11 PM
Thanks for the reply.
I am using <iostream>, but I keep getting the error:
error C2593: 'operator <<' is ambiguous.
any idea?

Regards,
Guy

kasracer
April 26th, 2004, 01:12 PM
Post the code you're using to print an _int64

Guysl
April 26th, 2004, 01:32 PM
#include <iostream>

int main(int argc, char*argv[])
{
__int64 myInt = 9223372036854775807;
std::cout << "Big number: " << myInt << std::endl;

return 0;
}


I have a general problem with iostream, since I cant std::cout<<string(), but I have to use std::cout<<string().c_str()


Regards,
Guy

jlou
April 26th, 2004, 02:13 PM
I get the same error (error C2593: 'operator <<' is ambiguous) only if I use <iostream.h> instead of <iostream>.

I also personally use a different standard library implementation from Dinkumware than the one that ships with VC++ 6. I didn't see anything in their documentation about fixing it for __int64, though. I also think that strings should print fine with the proper headers in any version.

Did you get any other errors or warnings (even unrelated) with the code you compiled?

kasracer
April 26th, 2004, 02:42 PM
I get a different error with MinGW. it says your number is too large for a long. Nothing about an ambiguous call

:confused:

Guysl
April 26th, 2004, 04:31 PM
kasracer,
try __int64 myInt = 9223372036854775807I64

Guysl
April 26th, 2004, 07:32 PM
I reinstalled VC++, and problems I had with <iostream> are fixed, but still, I get 'operator <<' is ambiguous' when using __int64.
does anyone except jlou that uses Dinkumware, can make it work?

Thanks in advance,
Guy

kasracer
April 26th, 2004, 07:49 PM
Originally posted by Guysl
kasracer,
try __int64 myInt = 9223372036854775807I64 invalid integer suffix

Guysl
April 26th, 2004, 08:04 PM
apparently, on VC++ 6 this case is reported as a bug.
The workaround is overloading operator<<.

link:
http://support.microsoft.com/default.aspx?scid=kb;en-us;168440

Mick
April 26th, 2004, 08:06 PM
http://www.kbalertz.com/Feedback_168440.aspx as stated somewhere up above in a reply.

Mick
April 26th, 2004, 08:07 PM
Originally posted by Guysl
apparently, on VC++ 6 this case is reported as a bug.
The workaround is overloading operator<<.

link:
http://support.microsoft.com/default.aspx?scid=kb;en-us;168440

meh beat me too it :p

kasracer
April 26th, 2004, 08:10 PM
Time to upgrade from the crap-tastic compiler! :p

Guysl
April 26th, 2004, 08:21 PM
Mick,

it was me above that used that workaround, at that time
I thought its was silly... :D

operator>> overloading though, is more dirty. should I parse
a temporary string for non-digit characters and raise exception if
I find any?

Mick
April 26th, 2004, 08:46 PM
Originally posted by Guysl
operator>> overloading though, is more dirty. should I parse
a temporary string for non-digit characters and raise exception if
I find any?

Not sure what you mean, your just providing an overload for the _int64 type. Non-digit? Exceptions?

Guysl
April 26th, 2004, 08:49 PM
I need to overload istream operator>> for __int64 type as well,
so I guess I have to implement legality checks.

dude_1967
April 27th, 2004, 11:40 AM
Yeah,

There is a mild compiler issue here, as indicated by others.

My standard solution is to just check for the compiler version and implement a workaround similar to the described workaround from the Microsoft page.

This workaround, written in this fashion is then 'portable', in that only the relevant compiler gets the workaround.

Sincerely, Chris.



#if defined(_MSC_VER) && (_MSC_VER <= 1200)

// Overwrite global operators << and >> for MSVC 6.0 with 64-bit integer type.
inline std::ostream& operator<<(std::ostream& os, const __int64& n)
{
char c[20];
::_i64toa(n, c, 10);
return os << c;
}

inline std::istream& operator>>(std::istream& is, __int64& n)
{
char c[20];
is >> c;
n = ::_atoi64(c);
return is;
}

#endif // IOstream fix for MSVC6

Guysl
April 27th, 2004, 12:54 PM
Thanks Chris. :thumb:


Regards,
Guy