This code example has been causng me no end of trouble.
Some of the errors are included in comments.
//EDIT: I changed the code to working code, but now i get a linker error:
c:\kyle\c++\char map\test.o(.text$initialize__17binary_conversion+0 xee):test.cpp: undefined reference to `binary_conversion::to_hex'
Code:
#include <iostream>
struct char_to_bin {
unsigned b1: 1;
unsigned b2: 1;
unsigned b3: 1;
unsigned b4: 1;
unsigned b5: 1;
unsigned b6: 1;
unsigned b7: 1;
unsigned b8: 1;
};
struct char_to_hex {
unsigned n1: 4;
unsigned n2: 4;
};
union char_bin_hex {
char ch;
struct char_to_bin bits;
struct char_to_hex hexs;
};
class binary_conversion {
private:
union char_bin_hex cbh;
char ch;
char binary[9];
char hexadecimal[5];
static const char to_hex[] = {'0','1','2','3','4','5',
'6','7','8','9','A','B',
'C','D','E','F'};
public:
binary_conversion(char in_ch){ch = in_ch; initialize();}
~binary_conversion(){}
void set_ch(char in_ch){ch = in_ch; initialize();}
void set_bin(const char in_bin[9]){} // NOT COMPLETED
void set_hex(const char in_hex[5]){} // NOT COMPLETED
char get_ch(){return ch;}
char *get_bin(){return binary;}
char *get_hex(){return hexadecimal;}
private:
void initialize(){
// Create binary string
sprintf(&binary[0],"%i",cbh.bits.b1);
sprintf(&binary[1],"%i",cbh.bits.b2);
sprintf(&binary[2],"%i",cbh.bits.b3);
sprintf(&binary[3],"%i",cbh.bits.b4);
sprintf(&binary[4],"%i",cbh.bits.b5);
sprintf(&binary[5],"%i",cbh.bits.b6);
sprintf(&binary[6],"%i",cbh.bits.b7);
sprintf(&binary[7],"%i",cbh.bits.b8);
binary[8]='\0';
// Create hex string
hexadecimal[1]=to_hex[cbh.hexs.n1]; // i think it complains here
hexadecimal[2]=to_hex[cbh.hexs.n2]; // or here
hexadecimal[3]='\0';
}
};
void binary(){
binary_conversion ch = (char)getche();
cout << ch.get_bin() << endl;
cout << ch.get_hex() << endl;
if (getche());
}
// END EDIT
If anyone could let me know of any problems you see, i would appreciate it greatly.
~Inquirer