Ambiguous << error

This is a discussion on Ambiguous << error within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write an __int64 to file and I'm coming up with an "ambiguous <<" error, but only on ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    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();
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    operator << doesnt exist for __int64. There is no single best conversion hence the error.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Ambiguous << error

    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....

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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
    Last edited by Stoned_Coder; 01-13-2003 at 12:11 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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;
    }

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >operator << doesnt exist for __int64.

    It does in VC++.net. It just uses a more complicated version of Fordys cheat.
    Joe

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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
    Last edited by Sang-drax; 01-13-2003 at 03:40 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Hehe, I was in the process of changing it while you posted
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21