-
Ambiguous << error
I'm trying to write an __int64 to file and I'm coming up with an "ambiguous <<" error, but only on the line that tries to save the __int64. Anyone know how to get around this? Here is a small snippet of the code:
Code:
void board::save(char *fileName, __int64 bName)
{
ofstream outFile;
outFile.open(fileName, ios::app);
outFile<<" ";
outFile<<bName;
outFile.close();
}
-
operator << doesnt exist for __int64. There is no single best conversion hence the error.
-
Re: Ambiguous << error
Quote:
Originally posted by PJYelton
I'm trying to write an __int64 to file and I'm coming up with an "ambiguous <<" error, but only on the line that tries to save the __int64. Anyone know how to get around this? Here is a small snippet of the code:
Code:
void board::save(char *fileName, __int64 bName)
{
ofstream outFile;
outFile.open(fileName, ios::app);
outFile<<" ";
outFile<<bName;
outFile.close();
}
Yes overload operator<< for an ostream and an __int64.....split the __int64 into 2 DWORDs and send them into the stream....
-
i think its a little more involved than that. this wont work for instance.
Code:
std::ostream& operator << (std::ostream& os,__int64& bigint)
{
__int32 top = (bigint >> 32) & 0xffffffff;
__int32 bottom = bigint & 0xffffffff;
(top==0)? os<<bottom:os<<top<<bottom;
return os;
}
edit....
actually something along these lines with a corresponding operator >> for reading in from a file should work but it would not work for printing to cout. if you only want file use and no need to print to cout then this could be good enough
-
Quote:
Originally posted by Stoned_Coder
i think its a little more involved than that. this wont work for instance.
Code:
std::ostream& operator << (std::ostream& os,__int64& bigint)
{
__int32 top = (bigint >> 32) & 0xffffffff;
__int32 bottom = bigint & 0xffffffff;
(top==0)? os<<bottom:os<<top<<bottom;
return os;
}
edit....
actually something along these lines with a corresponding operator >> for reading in from a file should work but it would not work for printing to cout. if you only want file use and no need to print to cout then this could be good enough
Yeah...maybe my answer was a bit too brief and narrow....I didnt think it through too well....actually it works ok if you normally count in hex (looks to adrian :) ).....
You can cheat though
Code:
#include <iostream>
#include <windows.h>
std:: ostream& operator<<(std:: ostream& out,const __int64& i64){
char buff[21];
wsprintf(buff,"%I64d",i64);
out << buff;
return out;
}
int main( void )
{
__int64 i64 = 50000000000000;
std::cout << i64 << std::endl;
return 0;
}
-
>operator << doesnt exist for __int64.
It does in VC++.net. It just uses a more complicated version of Fordys cheat.
-
Shoudn't something like this work?
Code:
std:: ostream& operator<<(std:: ostream& out,const __int64& i64){
__int64 num = i64;
std::stringstream str;
if (num<0)
num = -num;
if (num == 0)
out << '0';
while (num != 0)
{
str << int(num%10);
num/=10;
}
std::string s = str.str();
std::reverse(s.begin(),s.end());
if (i64<0)
out << '-';
out << s;
return out;
}
EDIT: Ooops, forgot to reverse order (I should compile before posting)
Now it should work
-
Sang-drax, wouldn't that print it out backwards? Not that it matters, I guess one would just reverse it when loading back in with another function.
Thanks for the help guys. I did some searching and asking around and I think I'll just save to binary with the read and write commands which should work and will actually make things easier for the rest of my program.
-
Hehe, I was in the process of changing it while you posted :)