Seperate class files, not being declared in scope?

Seperate class files, not being declared in scope? Scroll down to the bottom to see the errors.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include "include/Player.h"

using namespace std;

int main()
{
    bool GameActive = true;

    while(GameActive)
    {
        return 0;
    }
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player();
        ~Player();

        string getName();
        int Gold(string OP, int value);
        int Level(string OP, int value);
    private:
        string name;
        int gold = 0;
        int level = 1;
};

Player User;

#endif // PLAYER_H 


Player.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "../include/Player.h"
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

Player::Player()
{
    system("CLS");
    ifstream save("RPG - SAVE.txt");
    if(save.is_open())
    {
        User.name = getLine(save, 0);
        User.gold = getLine(save, 1);
        User.level = getLine(save, 2);
        save.close();
        cout << "Loaded your last save!" << endl;
    }

    else
    {
        cout << "Hello, you seem to be new here!" << endl;
        cout << "What is your name?" << endl;
        cin >> User.name;
        cout << "Well, hello " << User.name << "!" << endl;
        system("PAUSE");
    }
}

Player::~Player()
{
    ofstream save ("RPG - SAVE.txt");
    if(save.is_open())
    {
        save << User.getName() << endl;
        save << User.Gold("get") << endl;
        save << User.Level("get") << endl;
        save.close();
    }
}

string Player::getName()
{
    return User.name;
}

int Player::Gold(string OP, int value)
{
    if(OP == "get" || OP == "value")
    {
        return User.gold;
    }

    else if(OP == "add")
    {
        User.gold += value;
    }

    else if(OP == "subtract" || OP == "remove")
    {
        User.gold -= value;
    }

    else if(OP == "set")
    {
        User.gold = value;
    }

    else
    {
        cout << "Error: GG455" << endl;
    }
}

int Player::Level(string OP, int value)
{
    if(OP == "get" || OP == "value")
    {
        return User.level;
    }

    else if(OP == "add")
    {
        User.level += value;
    }

    else if(OP == "subtract" || OP == "remove")
    {
        User.level -= value;
    }

    else if(OP == "set")
    {
        User.level = value;
    }

    else
    {
        cout << "Error: LG455" << endl;
    }
}


When I run that, I get:
||=== RPG, Debug ===|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|18|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|19|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp||In constructor 'Player::Player()':|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|14|error: 'getLine' was not declared in this scope|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp||In destructor 'Player::~Player()':|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|37|error: no matching function for call to 'Player::Gold(const char [4])'|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|37|note: candidate is:|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|14|note: int Player::Gold(std::string, int)|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|14|note: candidate expects 2 arguments, 1 provided|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|38|error: no matching function for call to 'Player::Level(const char [4])'|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|38|note: candidate is:|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|15|note: int Player::Level(std::string, int)|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\..\include\Player.h|15|note: candidate expects 2 arguments, 1 provided|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp||In member function 'int Player::Level(std::string, int)':|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|102|warning: control reaches end of non-void function [-Wreturn-type]|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp||In member function 'int Player::Gold(std::string, int)':|
H:\Documents and Settings\Hank McDonald\My Documents\CodeBlocks\RPG\src\Player.cpp|74|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 3 errors, 4 warnings (0 minutes, 3 seconds) ===|
First thing's first. I think getLine is supposed to be getline (unless you have a function of your own defined or is a Windows API call). Second it is because your functions are declared with (string OP, int value) on both and you are only pass "get" to both. You have to pass ("get", 8) where eight is some number. Try that first and see if it helps.

The Errors in question:
1
2
save << User.Gold("get") << endl;
save << User.Level("get") << endl;

Your declarations of them:
1
2
int Gold(string OP, int value);
int Level(string OP, int value);
Last edited on by closed account z6A9GNh0
Both those solutions didn't help, the results are still the same.
Here is how to use getline() => http://www.cplusplus.com/reference/string/string/getline/

Here is the code fixed, I moved a few things around and commented out the getline. Had to include the system header to keep errors to a minimum.

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
#include <iostream>
#include <string>
#include "player.h"

using namespace std;

int main()
{
    bool GameActive = true;
    int num = 0;
    Player User;

    while(GameActive)
    {
		
		
		
		cout << "Enter a number: ";
		cin >> num;
		if(num == 999){ GameActive = false; }
    }
    
    return 0;
}

player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player();
        ~Player();

        string getName();
        int Gold(string OP, int value);
        int Level(string OP, int value);
    private:
        string name;
        int gold = 0;
        int level = 1;
};
#endif // PLAYER_H 

player.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "player.h"
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
//#include <windows.h>

using namespace std;

Player::Player()
{
    system("clear");
    ifstream save("RPG - SAVE.txt");
    if(save.is_open())
    {
        //getline();
        //getline();
        //getline();
        save.close();
        cout << "Loaded your last save!" << endl;
    }

    else
    {
        cout << "Hello, you seem to be new here!" << endl;
        cout << "What is your name?" << endl;
        cin >> this->name;
        cout << "Well, hello " << this->name << "!" << endl;
        system("PAUSE");
    }
}

Player::~Player()
{
    ofstream save ("RPG - SAVE.txt");
    if(save.is_open())
    {
        save << this->getName() << endl;
        save << this->Gold("get", 10) << endl;
        save << this->Level("get", 10) << endl;
        save.close();
    }
}

string Player::getName()
{
    return this->name;
}

int Player::Gold(string OP, int value)
{
    if(OP == "get" || OP == "value")
    {
        return this->gold;
    }

    else if(OP == "add")
    {
        this->gold += value;
    }

    else if(OP == "subtract" || OP == "remove")
    {
        this->gold -= value;
    }

    else if(OP == "set")
    {
        this->gold = value;
    }

    else
    {
        cout << "Error: GG455" << endl;
    }
}

int Player::Level(string OP, int value)
{
    if(OP == "get" || OP == "value")
    {
        return this->level;
    }

    else if(OP == "add")
    {
        this->level += value;
    }

    else if(OP == "subtract" || OP == "remove")
    {
        this->level -= value;
    }

    else if(OP == "set")
    {
        this->level = value;
    }

    else
    {
        cout << "Error: LG455" << endl;
    }
}
Last edited on by closed account z6A9GNh0
Topic archived. No new replies allowed.