This is for a project I am doing for my college class and I could not find an answer for this that worked for me anywhere. Its very likely I did not understand the answers though.
I have a struct (menuItem), and that struct is put into a class (Menu). I have successfully created an array of struct menuItem inside class Menu before, but when I try to create a vector of menuItem inside Menu I get this error when I try to initialize its size
error C2059: syntax error : 'constant'
This is the offending code:
#include <vector>
using namespace std;
//previous array value
const int MAXCOUNT = 20;
struct menuItem
{
void(*func)();
char description[50];
};
class Menu
{
private:
std::vector<menuItem> mi(20); // <<< "The error occurs when I set the vector to a size of 20"
int count;
void runSelection();
};
This error does not pup up when I initialize the vector as sizeless, but it pops up once again the second I use mi.resize(20); in the form of this contextual error: "Error, this deceleration has no storage class or type specifier", which makes no sense because I set the type of the vector mi as type menuItem per how I believe vectors are initialized.
I am assuming I am initializing the vector wrong somehow, or I am setting up the struct wrong. I've found "answers" stating that structs are classes without a private section which I knew, and that you have to have a constructor to initialize a vector of structs, which I did not know. Problem is, no constructor I've come up with has made the error go away. I am completely lost and would appreciate some assistance.
code sampleshould be better to read.mi.resize(20);outside of a function.