Menu Application Won't Compile

Jan 29, 2017 at 9:39pm
Ok, so I have a project for school that I've been trying to debug myself, but I can't get any further. This is a simple Menu application that will eventually be able to add more items. I can't get it to run, but I've been able to bring it down to 4 errors, and they're all in Menu.h:

Line 19: expression must have constant value
Line 12: 'descript': unknown override specifier
Line 12: missing type specifier - int assumed. Note: C++ does not support default-int
Line 19: expression did not evaluate to a constant

I have a feeling the answer is something really obvious, but I can't see it myself. Thank you in advance!

Menu.h
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
#ifndef MENU
#define MENU

#include <string>
#include <conio.h>

int maxCount=20;	//# Max menu items

struct menuItem
{
	void(*func)();	//Pointer to function
	string descript;	//Member function for menu item name

};

class Menu
{
private:
	menuItem mi[maxCount];
	int count;
	void runSelection();

public:
	Menu();
	void addMenu(char *Description, void(*f)());
	void runMenu();
	void waitKey();
};
#endif
Jan 30, 2017 at 12:42am
CobaltThunder wrote:
Line 19: expression must have constant value
Line 19: expression did not evaluate to a constant

The size of an array (except for dynamically allocated arrays) has to be a compile-time constant. Making maxCount a constant should fix this error.

 
constexpr int maxCount = 20;


CobaltThunder wrote:
Line 12: 'descript': unknown override specifier
Line 12: missing type specifier - int assumed. Note: C++ does not support default-int

The string class from the <string> header is inside the std namespace so you have to write std::string.

 
std::string descript;
Jan 30, 2017 at 5:03am
Ok! The errors are gone and the program compiles, but when I attempt to run it, it gives me this error in the dialogue window:

'"c:\users\<name>\documents\visual studio 2017\Projects\MainMenu.exe"' is not recognized as an internal or external command, operable program or batch file.

So close I can taste it =(
Jan 30, 2017 at 9:17am
Do you have a file containing a main function? Make sure you build the whole project and not just a single file.
Jan 31, 2017 at 4:25am
Yes, I do. I have a Menu.h, Menu.cpp, and a main.cpp
Jan 31, 2017 at 10:52am
And when you (re)build the whole project you don't get any errors?
Feb 14, 2017 at 6:10am
No, there are no errors. It compiles, but when the console window opens, it gives that message.
Feb 14, 2017 at 6:25am
Ok, I don't know what changed, but I went back through and was able to resolve what errors I was now getting. My problem now is that the console window prints nothing but empty space, which I can interact with for about 5 seconds before getting the "press any key to continue" message, which closes the window. Nearly there!

Here is my complete 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
#ifndef MENU
#define MENU

#include <string>
#include <conio.h>

using std::string;

constexpr int maxCount = 20;	//# Max menu items

struct menuItem
{
	void(*func)();	//Pointer to function
	string descript;	//Member function for menu item name

};

class Menu
{
private:
	menuItem mi[maxCount];
	int count;
	void runSelection();

public:
	Menu();
	void addMenu(char *Description, void(*f)());
	void runMenu();
	void waitKey();
};
#endif 


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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "Menu.h"

using namespace std;

Menu::Menu()
	:count(0)
{

}

void Menu::addMenu(char *Description, void(*f) (void))
{

	if (count < maxCount)
	{
		this->mi[count].func = f;
		using std::string;(this->mi[count].descript, Description);
		count++;
	}
}

void Menu::runMenu()
{
	for (;;)
	{
		system("CLS");
		for (int i = 0; i < count; i++)
		{
			cout << this->mi[i].descript << endl;
		}
		runSelection();
	}
}

void Menu::runSelection()
{
	int select;
	cin >> select;

	if (select <= count)
		this->mi[select - 1].func();
}

void Menu::waitKey()
{
	cout << "Press any key to continue" << endl;
	while (!kbhit());
	fflush(stdin);
}


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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "Menu.h"

using namespace std;

void func1();

void func2();

void func3();

void Exit();

Menu m;

int main()

{

	m.addMenu("1. Function1 ", func1);
	m.addMenu("2. Function2 ", func2);
	m.addMenu("3. Function3 ", func3);
	m.addMenu("4. Exit ", Exit);

	m.runMenu();
}

void func1()
{
	cout << "This is func 1" << endl;
	m.waitKey();
}

void func2()
{
	cout << "This is func 2" << endl;
	m.waitKey();
}

void func3()
{
	cout << "This is func 3" << endl;
	m.waitKey();
}

void Exit()
{
	cout << "Goodbye" << endl;
	exit(0);
}
Feb 14, 2017 at 9:57am
This line doesn't do anything useful.
 
using std::string;(this->mi[count].descript, Description);
Topic archived. No new replies allowed.