Class Error

Getting the error: "Error: too many initializer values"

Can someone help me figure out what I'm doing wrong?

Code:

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
34
35
36
37
38
39
#include <iostream>

using namespace std;

bool chicken;
bool chips;
bool fish;

class Takeaway{
public:
	void WantsChicken(){
		chicken = true;
	}
	void WantsChips(){
		chips = true;
	}
	void WantsFish(){
		fish = true;
	}
};

int main()
{
	char yn1;
	char yn2;
	char yn3;

	Takeaway CustomerOne = { false, false, false };  // Getting the error here at the first false.

	cout << "Do you want chicken? (Y/N)" << endl;
	cin >> yn1;
	if (yn1 == 'y' || yn1 == 'Y')
	{
		CustomerOne.WantsChicken();
	}

	system("PAUSE");
	return 0;
}
Move the variables (chicken/chips/fish) inside the class Takeaway
Last edited on
What you want is a default class constructor. add this to your class:

Takeaway(bool wchicken= false, bool wchips= false, bool wfish= false) : chicken(wchicken), chips(wchips), fish(wfish)
{
}

Then change the line with the error to simply:

Takeaway Customerone;

This will initialize chicken, chips, fish as false. If you wanted, however, to initialize it otherwise:

Takeaway Customerone(true); // chicken would be true, chips and fish would be false
Takeaway Customerone(true,true); // chicken and chips would be true, fish would be false
Takeaway Customerone(true,true,true); // chicken, chips, and fish would be true

I'm sure you get the idea.
Thanks coder777 it worked.

Simple enough, however why do they need to be in there?

I thought variables put above would be universal for the entire program.
I thought variables put above would be universal for the entire program.
Yes, they're called global variables and as such they don't have anything to do with any class whatsoever.

Having a constructor (like Texan40 stated) is the recommended way to initialize an object like this.
Topic archived. No new replies allowed.