Using vector inside a union

This is a discussion on Using vector inside a union within the C++ Programming forums, part of the General Programming Boards category; Hi I was using a vector inside a union. Is this legal? Thank you,...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Using vector inside a union

    Hi I was using a vector inside a union. Is this legal?

    Thank you,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Why do you want to do that in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To answer the strict question, any object that has a non-trivial constructor cannot be in a union, and I believe std::vector has a non-trivial constructor.

    But this really seems like there's a more normal way to do whatever it is you want to do.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    I am trying to write a yacc parser and was trying to debug the issue where the parser run for a long time but the output stop after a certain point. Beside this I don't know what else I should do. Below is my code. Would you please help
    Code:
    %{
       #include <stdlib.h>
       #include <stdio.h>
       #include <string.h>
       #include "defs.h"
       #include "Geom.h"
       #include "dbLayer.h"
       #include "Term.h"
       #include "Cell.h"
       #include "dbNet.h"
       #define YYMAXDEPTH 10000
       #define YYDEBUG 1
       int yylex(void);
       void yyerror(char *);
       extern int linecount;
       extern int yylex();
       extern int yyparse();
       extern FILE *yyin;
       extern FILE* yyout;
       static std::map<char*,dbNet*>NET_HASH;
       //static std::map<char*,dbNet*>::iterator NET_ITER;
       static std::map<char*,dbMacro*>MACRO_HASH;
       //static std::map<char*,dbMacro*>::iterator MACRO_ITER;
    %}
    
    %union {
       int ival;
       float fval;
       char* sval;
       int pindir;
       std::vector<char*>* charportlist;
    };

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I have no idea, and don't want to think about, whether a pointer to a vector is allowed in a union. Yacc and I are not really on speaking terms, so I can't provide debugging help; but I don't really like the looks of that union. What (if anything) is it supposed to represent?

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Yeah I think I have to remove it and define it somewhere else. I am new to this yacc thing so I might do something stupid here. Anyway, thanks you guys for directing to the right direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to union inside struct
    By lv2eof in forum C Programming
    Replies: 4
    Last Post: 05-28-2010, 06:55 PM
  2. union inside structure
    By vijay s in forum C Programming
    Replies: 1
    Last Post: 12-03-2009, 06:21 AM
  3. Keep track of elements inside STL vector
    By Krones in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2009, 12:39 PM
  4. Storing a vector inside a vector
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 05:15 AM
  5. vector - left of .push_back must have class/struct/union type
    By patricio2626 in forum C++ Programming
    Replies: 5
    Last Post: 11-18-2006, 03:37 PM

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