Stuck with a vector

Hello everyone, I'm currently working on a school assignment, and for the life of me can't figure out what I'm doing wrong.

We had to create a menu using an array, and then later change it to a vector.
Here is my current code which is giving me errors.

Header
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
#ifndef MENU
#define MENU

#include <string>
#include <vector>
using std::string;
using std::vector;


struct menuItem
{
	void(*func) ();
	string descript;

};

class Menu
{
private:
	vector<menuItem> mi;
	int count;
	void runSelection();


public:
	Menu();
	void addMenu(string descript, void(*f) ());
	void runMenu();
	void waitKey();
};






#endif 

CPP file
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
51
52
53
54
55
56
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <vector>

using std::vector;
using namespace std;

#include "Menu.h"

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

}
void Menu::addMenu(string descript, void(*f) (void))
{
	if (count < mi.size())
	{
		mi[count].func = f;
		mi[count].descript = descript;
		count++;

	}

}

void Menu::runMenu()
{
	for (;;)
	{
		system("CLS");
		for (int i = 0; i < count; i++)
		{
			cout << mi.push_back(i) << 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);
}



Line 35 of the CPP file is giving me an error, and overall I'm not certain the runMenu function is correct either, even though it doesn't give an error.
Last edited on
It looks like line 35 should be: cout << mi[i] << '\n'; You're trying to print out the contents of the vector, not add elements to it. std::vector has this method called size which you should probably use (there is no reason to keep your count member.)

1
2
3
4
5
void Menu::addMenu(string descript, void(*f) (void))
{
    menuItem item = { f, descript };
    mi.push_back(item);
}
Thanks Cire!
However on this line cout << mi[i] << '\n'
I get this error.
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'menuItem' (or there is no acceptable conversion)

These are my includes.
1
2
3
4
5
6
7
8
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <vector>
#include "Menu.h"

using std::vector;
using namespace std;
You're trying to cout a class, which won't work. You need to do something like this.

 
cout << this->mi[i].descript << endl; //whatever you want to be displayed 
Topic archived. No new replies allowed.