Beginner Class Help Needed

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
#include <iostream>
#include <string>

using namespace std;

class safe {

    public:
        string giveCode() {
            return code1; 
        }
    private:
        int code1; 
        code1 = 1337;
}

int main() {
    int x;
    Combo:
    cout << "Please imput combination";
    cin>>x; 
    if (x == 1234) 
        cout << giveCode();
    else
        cout << "Wrong combination";
        goto Combo;

    
}



ISO C++ forbids declaration of `code1' with no type 
ISO C++ forbids initialization of member `code1' 


There was more output than that, and I'm pretty sure I did a LOT wrong
You forgot the ; at the end of the class declaration.

You are not allowed to define values when first setting up the class (as you did in the private section).

A better way is to define it in the constructor instead.
 
safe() {code1 = 1337;}

http://www.cplusplus.com/doc/tutorial/classes/

Your "giveCode()" function has a return type of string, however code1 is an int, so make sure the return type matches the variable.
You haven't created an instance of the class safe - that is you didn't make a safe object.

You also have a goto - which is really bad - forget that they even exist - use a while loop instead.
Topic archived. No new replies allowed.