'fruitList' does not name a type

I am trying to create a list in a class, but when I add items to that list, it receives an error, stating "fruitList does not name a type". Please help!

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
40
#include <iostream>
#include <list>

using namespace std;

class Menu
{
	public:
		Menu();
		virtual ~Menu();
		string getList() { return "The Menu categories are:\n\t1. Fruits\n\t2. Vegetables\n\t3. Grain\n\t4. Dairy\n\t5. Meats"; }
};

class Fruits : public Menu
{
		list<string> fruitList;
		fruitList.push_back("Blueberry");
	public:
		
};

int main()
{
	
	Menu menu;
	cout << menu.getList() << endl;
	
	
	
	
	return 0;
}

Menu::Menu()
{
}

Menu::~Menu()
{
}
We usually put executable statements in functions.
Hello TheDomesticUser2,

Your program uses "std::string"s, so you need to include the header file "string".

In the class "Menu" I have not seen "virtual ~Menu();" used before. I believe that you can drop the "virtual". Also since neither the ctor or dtor do anything you can drop the forward declaration and the functions and let the compiler make defaults for you.

In the "Fruits" class this inherits the public member functions of the "Menu" class. Line 17 does not work here. This line is better put in the ctor. that you do not have.

In "main" you are creating an object of the wrong class. It should be "Fruits" not "Menu". Then line 26 would be cout << fruits.getList() << endl;. Which works because when you create the object of "Fruits" it also creates an object of "Menu" to give you access to "Menu"'s public functions.

Hope that helps,

Andy
Topic archived. No new replies allowed.