Accessing stuff from class

Ok so i hasve a class and i cant access a string or another variable for some reason, what am i doing wrong? im trying to use name and age in main. also in save.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>

using namespace std;

class Vars
{
    public:
        void Bank();
        void Save();
        Vars()
        {
            money = 0;
            level = 1;
            name;
            age;
        }
    private:
        int money;
        int level;
        int age;
        string name;
};


int main()
{
    Vars VO;
    VO.age;
    string choice;

    cout << "New" << endl;
    cout << "Load\n" << endl;
    cin >> choice;

        if(choice == "New" || choice == "new")
        {
            cout << "Please enter your name\n" << endl;
            cin.ignore(556, '\n');
            getline(cin, name);
            cout << "\n";

            cout << "Ok thanks " << name << " What is your age?\n" << endl;
            cin >> age;
            cout << "\n";

            cout << "Ok so your name is " << name << " and your " << age << " years old" << endl;

            cin.get();

            VO.Bank();
        }
        else if(choice == "Load" || choice == "load")
        {

        }
}


void Vars::Bank()
{

}

void Vars::Save()
{
    Vars VO;

    ofstream file("Bank.txt");

    file << name << endl;
    file << age << endl;
}



C:\Users\ellie\Desktop\Bank teller\main.cpp||In constructor 'Vars::Vars()':|
C:\Users\ellie\Desktop\Bank teller\main.cpp|15|warning: statement has no effect|
C:\Users\ellie\Desktop\Bank teller\main.cpp|16|warning: statement has no effect|
C:\Users\ellie\Desktop\Bank teller\main.cpp||In function 'int main()':|
C:\Users\ellie\Desktop\Bank teller\main.cpp|21|error: 'int Vars::age' is private|
C:\Users\ellie\Desktop\Bank teller\main.cpp|29|error: within this context|
C:\Users\ellie\Desktop\Bank teller\main.cpp|29|warning: statement has no effect|
C:\Users\ellie\Desktop\Bank teller\main.cpp|40|error: 'name' was not declared in this scope|
C:\Users\ellie\Desktop\Bank teller\main.cpp|44|error: 'age' was not declared in this scope|
C:\Users\ellie\Desktop\Bank teller\main.cpp||In member function 'void Vars::Save()':|
C:\Users\ellie\Desktop\Bank teller\main.cpp|69|error: variable 'std::ofstream file' has initializer but incomplete type|
||=== Build finished: 5 errors, 3 warnings ===|
Last edited on
Do you know what public, protected and private do?
Im familiar with public and protected but not sure about private, ever heard of it before.
¿why do you use it, then?
Last edited on
Try going through the errors and warnings one by one for starters?

Line 15: Just naming a variable and doing nothing has no use at all. You're not initializing it nor declaring it.
Line 16: Same as line 15.

Line 21: You declared money, level, age and name as private. As kbw said, look up what public, protected and private do.

Line 29: Bit like 15 and 16, you just name a variable from an object, you don't change it, you don't output it, just named it.

Line 40: You try to copy user input into a non-existing variable. Either declare the variable, or (as you probably wanted), use the object's name property (after fixing the private issues in your class).
Line 44: Same as line 40.

Line 69: You need to #include <fstream>
or no i meant protected sorry. ill look at my errors and try to fix them
Topic archived. No new replies allowed.