Object and variable not declared in scope

Hi all

I'm brand new to C++ and am having a few problems with my basic understanding of how scoping works. I've done searches and am going through the tutorials section but if someone could tell me what I'm doing wrong here I think it would help my understanding immensely.

I'm trying to ascertain and then set a player's name for a specific instantiation of the class Player. The classes are a bit convoluted, as I intend to do much more in the long run.

This my main...

1
2
3
4
5
6
7
8
9
10
11
#include "PlayGame.h"

using namespace std;

int main() {

    PlayGame newPlayGame;
    newPlayGame.myGame();

    return 0;
}


Which sets up an instance of PlayGame:

Header
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef PLAYGAME_H
#define PLAYGAME_H

class PlayGame
{
    public:
        PlayGame();
        void myGame();

};

#endif // PLAYGAME_H 


Body
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "PlayGame.h"

using namespace std;

PlayGame::PlayGame() {
}

void PlayGame::myGame() {

    Player newPlayer;
    newPlayer.playerIntro();

}


Which then goes on to call playerIntro for the newPlayer object in the Player class:

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

#include <string>

class Player
{
    public:
        Player();
        void playerIntro();
        void setPlayerName(std::string inputName);

    private:
        std::string _name;
};

#endif // PLAYER_H 


Body
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Player.h"

#include <iostream>
#include <cstdio>

using namespace std;

Player::Player() {
}

void playerIntro() {

    string inputName;
    printf("Please enter your name: ");
    cin >> inputName;
    setPlayerName(inputName);

}

void setPlayerName(string inputName) {

    _name = inputName;

}


This gives me two errors:

1
2
3
4
5
6
||=== Build: Debug in myGame (compiler: GNU GCC Compiler) ===|
C:\Users\.....\Player.cpp||In function 'void playerIntro()':|
C:\Users\.....\Player.cpp|16|error: 'setPlayerName' was not declared in this scope|
C:\Users\.....\Player.cpp||In function 'void setPlayerName(std::string)':|
C:\Users\.....\Player.cpp|22|error: '_name' was not declared in this scope|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


setPlayerName and _name are both declared in the header and I only want to set the player name for that particular Player object.

I would appreciate some advise on why these are out of scope and how to declare them in scope.

Thanks in advance.

p.s. for some reason I can't get preview to work so I hope the code tags take.
Last edited on
closed account (EwCjE3v7)
You need to declare your functions, classes, variable etc... before using them.

There are two ways, you can either declare your function before using it

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
#include "Player.h"

#include <iostream>
#include <cstdio>

using namespace std;

Player::Player() {
}

void setPlayerName(string); // declaration

void playerIntro() {

    string inputName;
    printf("Please enter your name: ");
    cin >> inputName;
    setPlayerName(inputName);

}

void setPlayerName(string inputName) {

    _name = inputName;

}


or you can define and declare it before using it

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

#include <iostream>
#include <cstdio>

using namespace std;

Player::Player() {
}

void setPlayerName(string inputName) { // definition and declaration

    _name = inputName;

}

void playerIntro() {

    string inputName;
    printf("Please enter your name: ");
    cin >> inputName;
    setPlayerName(inputName);

}


Your second problem is that you are trying to access a private variable when you have no access from the function setPlayerName

There are two ways you can fix this, friend the function in your class or get a function in your class to return the variable..

Friend

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

#include <string>

class Player
{
    public:
        friend void setPlayerName(string); // friend the function so it can access _name
        Player();
        void playerIntro();
        void setPlayerName(std::string inputName);

    private:
        std::string _name;
};

#endif // PLAYER_H  
Last edited on
Cheers Fr0zen1, you're a star!

Just for the sake of my clarity was the problem with my original code for setPlayerName the fact that it was placed in the wrong order, as the function itself seems to be the same as yours for the definition and declaration approach.

1
2
3
4
5
6

void setPlayerName(string inputName) {

    _name = inputName;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void playerIntro() {

    string inputName;
    printf("Please enter your name: ");
    cin >> inputName;
    setPlayerName(inputName);

}

void setPlayerName(string inputName) {

    _name = inputName;

}


you are not defining your setplayername() and playerintro() in your player class
Topic archived. No new replies allowed.