no appropriate default constructor available

I am getting the following error when attempting to compile this program:

cpp(59): error C2512: 'SodaMachine::DrinkInfo' : no appropriate default constructor available

Here is the struct / class:

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
class SodaMachine
{
	private:
		struct DrinkInfo
		{
			string name;
			double price;
			int numDrinks;

		DrinkInfo(string n, double p, int numD)
		{
			name = "unknown";
			price = 0.00;
			numDrinks = 0;
		}
		};

		void inputMoney();
		void dailyReport();
		DrinkInfo drinks[DRINKS];
	public:
		void buyDrink(int);
		void displayChoices();
		SodaMachine();
};


And here is my attempt to initialize the array in the class constructor:

1
2
3
4
5
6
7
8
9
10
/***************SodaMachine::SodaMachine*****************/
//Class constructor defines the soda struct array
SodaMachine::SodaMachine()
{
	drinks[0] = DrinkInfo("Cola", .75, 20);
	drinks[1] = DrinkInfo("Root Beer", .75, 20);
	drinks[2] = DrinkInfo("Orange Soda", .75, 20);
	drinks[3] = DrinkInfo("Grape Soda", .75, 20);
	drinks[4] = DrinkInfo("Bottled Water", 1.00, 20);
}


Anyone know what the problem is? I'm stumped.

Thanks
The object SodaMachine contains an array of DrinkInfo objects named drinks. The constructor for 'class SodaMachine' must explicitly initialize the member 'drinks', but there's no default constructor for DrinkInfo objects.

I think your DrinkInfo struct should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct DrinkInfo
{
  string name;
  double price;
  int numDrinks;

  DrinkInfo()
 {
	name = "unknown";
	price = 0.00;
	numDrinks = 0;
 }

 DrinkInfo(string n, double p, int numD)
 {
	name = n;
	price = p;
	numDrinks = numD;
 }
};


Alternatively, if you've got an up-to-date compiler, I think you can present the SodaMachine constructor with an initialiser list, but the syntax is probably a bit fiddly.
Last edited on
Topic archived. No new replies allowed.