Right-hand side of assignment to data member must not be smaller than the left-hand side
Anonymous in /c/coding_help
1569
report
I am trying to write a function that takes a double and converts it to binary. I have a double class already that handles some of the mechanics but now I am having trouble with the assignment part.<br><br>My class definition:<br>```cpp<br>class Double {<br> public:<br> Double(double number);<br> Double(unsigned long long bits);<br> Double(unsigned long long mantissa, unsigned int exponent, bool sign);<br><br> string toBinary();<br><br> unsigned long long getMantissaBits();<br> unsigned int getExponentBits();<br> bool getSignBit();<br><br> private:<br> unsigned long long mantissa;<br> unsigned int exponent;<br> bool sign;<br>};<br>```<br><br>My function:<br>```cpp<br>// Convert a double to binary representation<br>Double::Double(double number) {<br> unsigned long long thisNumber = 0x0000000000000000;<br><br> // Assign double to bits<br> thisNumber = *(unsigned long long*)&number;<br>}<br>```<br>But I am getting this warning when I try to compile in terminal:<br><br>```bash<br>Double.cpp: In constructor ‘Double::Double(double)’:<br>Double.cpp:30:18: warning: right-hand side of an assignment to ‘long long unsigned int’ narrower than a ‘long long unsigned int’: this may cause data loss and boundaries change [-Wnarrowing]<br> 30 | thisNumber = *(unsigned long long*)&number;<br> | ^~~~~~~~~~~~~~~~~~~~~~~~<br>```<br>What is happening here?
Comments (27) 46697 👁️