I am not even sure if my title is accurate, but for my project, I need to take a double and convert it into a hexadecimal and then convert it back to a double. But in order to do that, I think I would need to overload the operator << before I can output the answer. This is what I have so far, but I get prompted that "no operator "<<" match these operands"> Could someone please point me in the right direction of how I should overload the << operator in main? Thank you!
#include <iostream>
#include <iomanip>
#include <cstdint>
using namespace std;
void out_char_as_hex(int c)
{
cout << hex << setw(2) << setfill('0') << c;
}
int main()
{
union { double d; uint64_t u; } tmp;
double x = -0.15625;
tmp.d = x;
cout << out_char_as_hex(tmp.d) << endl;
return 0;
}
if it helps this is the question "If x is a variable of type double, it binary representation can be re-interpreted as a 64 bits integer, which can be represented exactly. To do that, you can either get the memory address to the variable of type double and reinterpret cast it to a 64-bit int pointer or use a union. To make the textual representation more compact, we use base 16 (hexadecimal). For example, the double number -0.15625 should be saved to file as the sequence of 16 characters bfc4000000000000 (see example in DemoHex.cpp, which use a union). When reading, you need to read the integer number saved in hexadecimal format and re-interpret it as a double. You need to modify the implementation of the operator<< for double and implement the overload of the operator>>"