How to pass an enum through a struct?

I have been trying to do this for about 10 minutes now and I cant seem to figure it out. I need to pass the enum through the struct parameters.

main.cpp

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
#include <iostream>
#include <string>
#include <fstream>
#include "Varstruct.h"
#include "Prototypes.h"

using namespace std;

 Vars::Vars(string NAME, Vars::NUM): name(NAME), num(NUM)
 {

 }

int main()
{
    Vars vars("string", 0);

    MAINPROGRAM(vars);
    return 0;
}

void MAINPROGRAM(Vars &vars)
{
    cout << "Please enter your name" << endl;
    getline(cin, vars.name);

    cout << "Hello " << vars.name << " now please enter your age: ";
    cin >> vars.age;

    cout << vars.name << ' ' << vars.age << endl;

    save(vars);
}

void save(Vars &vars)
{
    ofstream saveFile("file.txt");

    saveFile << vars.name << endl;
    saveFile << vars.age << endl;
}

void load(Vars &vars)
{
    ifstream loadFile("file.txt");

    loadFile >> vars.name;
    loadFile >> vars.age;
}


Prototypes.h

1
2
3
4
5
6
7
8
9
10
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

#include <string>

void save(Vars &vars);
void load(Vars &vars);
void MAINPROGRAM(Vars &vars);

#endif // PROTOTYPES_H_INCLUDED 



varstruct.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef VARSTRUCT_H_INCLUDED
#define VARSTRUCT_H_INCLUDED

#include <string>

struct Vars
{
    Vars(std::string NAME, Vars::NUM);

    std::string name;
    enum num
    {
        age,
        height
    };
};

#endif // VARSTRUCT_H_INCLUDED 
Last edited on
Make sure you use upper and lower case letters consistently. NUM is not the same as num.
I use the upper and lower the same in every spot, I just cant pass enum ans an argument.
In varstruct.h you use upper case on line 8 and lower case on line 11.

You also need to define the enum before you mention it in the code so you have to move the enum definition of num above the Vars constructor declaration.

Inside the scope of Vars (Inside the class definition and inside the constructor definition) you don't have to refer to it as Vars::num. You can simply refer to it as num.
In varstruct.h you use upper case on line 8 and lower case on line 11.

But string is in lowercase and then in uppercase and that works fine, so why shouldnt this?
That's the variable name. If you want to have a parameter of type num with the name NUM you do
 
num NUM
Topic archived. No new replies allowed.