Question about unions

This is a discussion on Question about unions within the C++ Programming forums, part of the General Programming Boards category; This one has stumped me a bit.... I have the following bit of code : Code: typedef struct sField { ...

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Question about unions

    This one has stumped me a bit....

    I have the following bit of code :
    Code:
    typedef struct sField
    {
        short x, y, length;
        short type;
        union data
        {
            short selection;
            std::string line;
        }
    };
    that produces this error message :
    Code:
    member 'sField::data::line' of union 'sField::data' has copy constructor
    Am I correct in assuming that a union member cannot contain a class object that has a defined copy constructor?

    If so, is there any way around this without using C-style strings? I would really like to use a std::string here as it would make the implementation a lot easier.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    What are you actually trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    It's going to eventually be part of a console based menu system. I am creating a templete class that will, among other things, allow the user to input selections for menu options. It will also allow the user to enter data into various forms and data structures. I figure that by using a union like this I can use a templete function to get user input and store it in the appropriate data type based on how the data is going to be used.

    It is still very much in the begining stages and the bit of code that I posted above will get expanded with more functionallity, but I need it to be very generic so that it can be used with different data types. The short and std::string that I have listed there is just the begining.

    This is also a bit of a learning exercise for me as well, so if you have and better or more efficient ideas they are most definitly welcome...
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't put non-POD types into a union.

    I am creating a templete class that will, among other things, allow the user to input selections for menu options.
    OK.

    It will also allow the user to enter data into various forms and data structures.
    Isn't the class going to a little do much? What does various forms and data structures have to do with a console menu?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems like a task much better suited for a set of classes that inherit from a(n abstract) base class, e.g. :
    Code:
    class BaseField
    { 
    protected:
        short x, y, length;
    public:
        virtual ~BaseField(); 
    };
    
    class ShortField: public BaseField
    {
    private:
        short d;
    public:
        short GetData() { return d; }
    };
    
    
    class StringField: public BaseField
    {
    private:
        std::string d;
    public:
        std::string GetData() { return d; }
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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