Putting std::string in union returns error

Hello,
So whenever I put std::string in a union it returns the following error: the default constructor of "storeItem" cannot be referenced -- it is a deleted function. This error is referring to storeItem lastItem, putting the union outside of main doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;



int main() 
{
	// Store Item Union
	union storeItem {
		string item_name;
		int price;
		bool default_item;
		~storeItem() {}
	};

	// Declare Store Variables

	int level;
	int xp;
	string storeName;
	string storeState;
	bool flag = true;

	storeItem lastItem;

	string userinput;

	// Declare Store Items Vector.
	vector<storeItem> storeItems;


Any help is appreciated.
Last edited on
Read this about union:

http://en.cppreference.com/w/cpp/language/union

It has rather limited capabilities. The main problem is that you cannot tell what member is used.

It looks like you want a struct not a union anyway.
Thanks, using struct fixed it.
Topic archived. No new replies allowed.