Compilation error for union

会有一股神秘感。 提交于 2019-12-11 11:22:27

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/22764587/compilation-error-for-union

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!