Thread: Class + Union + Struct = Disaster??

  1. #1

    Class + Union + Struct = Linker Disaster??

    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
    Last edited by Inquirer; 10-27-2002 at 08:46 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    • Add ; after the union and struct declarations.
    • Declare the structs and unions before the class, to make the class able to use them.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    hehe. how silly of me! thanks!

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #4
    Now i am getting an odd linker error. See the (edited) original post.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I´m not sure about this but only const data-members of integral type can be initialized within the class body. Try change it to

    Code:
    //In the class body
    //static const char to_hex[] = {'0','1','2','3','4','5', '6','7','8','9','A','B','C','D','E','F'};
    static const char to_hex[16];
    
    //And add this to initialize it, outside the class body
    const char binary_conversion::to_hex[16]='0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    Hope that it helps

  6. #6
    The only thing i could to to get it to work was make it global. If anyone else has a suggestion, let me know!

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  2. Problem with using a union into a struct :)
    By g_p in forum C Programming
    Replies: 11
    Last Post: 05-12-2007, 03:21 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM