Sumeet
Sumeet

Reputation: 779

Compilation error for union

can somebody please explain why the following program causing the compilation problem. I have compiled the source code over VS2013.

#include <iostream>
using namespace std;

// Do not work
union myuni
{
    string str;
};

void main()
{

}

Does union require the fixed length size while declaring it? The same scenario works fine with structure.

Upvotes: 1

Views: 93

Answers (1)

Bathsheba
Bathsheba

Reputation: 234795

You cannot have a string in a union as the former contains a constructor.

(Although allowed in C++11 this is not supported in VS2013).

Upvotes: 6

Related Questions