Havent been programming fr a month need some help lease

I havent been programming for over a montha nd i dont want to get too rusty so im just programming to keep my knoweledge fresh but that seems to have happened anyways. I have a class and am trying to use a string in my main program but i get errors.

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
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include "Player.h"
#include <string>

/*

int x = 800;
int *pointer = &x;

cout << *pointer << endl;

*/

void Load()
{

}

void Save()
{

}

void Game()
{

}

using namespace std;

int main()
{
    int choice = 0;

    Player PO;


    cout << "Warehouse Manager\n" << endl;

    cout << "1) New Game" << endl;
    cout << "2) Load Game" << endl;
    cin >> choice;

    if(choice == 1)
    {
        cout << "Welcome, what is your name?" << endl;
        getline(cin, PO.Player_Name);

        cout << "/nOk " << PO.Player_Name << " Lets get some info before we start" << endl;

    }
    if(choice == 2)
    {
        Game();
    }
}



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_INCLUDED
#define PLAYER_H_INCLUDED

#include <string>

class Player
{
    public:
        Player();

    private:
        int money;
        string Player_Name;
};

Player::Player()
{
    money = 0;
    Player_Name = "";
}

#endif // PLAYER_H_INCLUDED 


C:\Users\Chay\Desktop\help\Player.h|13|error: 'string' does not name a type|
C:\Users\Chay\Desktop\help\Player.h||In constructor 'Player::Player()':|
C:\Users\Chay\Desktop\help\Player.h|19|error: 'Player_Name' was not declared in this scope|
C:\Users\Chay\Desktop\help\main.cpp||In function 'int main()':|
C:\Users\Chay\Desktop\help\main.cpp|37|error: 'class Player' has no member named 'Player_Name'|
C:\Users\Chay\Desktop\help\main.cpp|49|error: 'class Player' has no member named 'Player_Name'|
C:\Users\Chay\Desktop\help\main.cpp|51|error: 'class Player' has no member named 'Player_Name'|
||=== Build finished: 5 errors, 0 warnings ===|
In Player.h on line 13 you declare Player_Name to be of type string. However, the compiler doesn't know what a string is. It knows what an std::string is, which is probably what you want. So either declare it with std::string as the type or stick using namespace std; in your .h file.
Maybe you need the line "using namespace std;" in your header file as well?
ok thanks now i get these errors

C:\Users\Chay\Desktop\help\Player.h||In function 'int main()':|
C:\Users\Chay\Desktop\help\Player.h|15|error: 'std::string Player::Player_Name' is private|
C:\Users\Chay\Desktop\help\main.cpp|47|error: within this context|
C:\Users\Chay\Desktop\help\Player.h|15|error: 'std::string Player::Player_Name' is private|
C:\Users\Chay\Desktop\help\main.cpp|49|error: within this context|
||=== Build finished: 4 errors, 0 warnings ===|
Yep, Player_Name is declared in the private section of class Player, and therefore inaccessible by others. Put it in the public section.
I dont like making my variables public, whats the other option.
Give your Player class get and set functions for Player_Name. Then in your main function, use

1
2
3
string temp;
getline(cin, temp);
PO.SetPlayerName(temp);

instead of

getline(cin, PO.Player_Name);
closed account (zb0S216C)
You're "#including <string>" on three occasions; you only need to "#include <string>" inside of Player.h.

@OP: Leave your data-members as private. What I recommend that you do is add a member-function which gathers input, like so:

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
bool const Player::Get_Input( )
{
  std::cout << "Welcome, what is your name? ";
  std::cout.flush( );

  std::getline( std::cin, Player_Name );
  if( std::cin.fail( ) )
  {
    // Handle error.
    std::cin.clear( std::ios::failbit ); // Don't have to do this.
    return( false );
  }

  std::cin.ignore( ); // Discard the new-line character.
  return( true );
}

int main( )
{
  ::Player Player_;
  if( !Player.Get_Input( ) )
  {
    // Handle error.
  }

  return( 0 );
}

With this approach, the class controls the input which helps to prevent a breach of encapsulation.

Wazzak
Last edited on
Ok well i seem to have fixed everything except one problem i cant figure out :(

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

/*

int x = 800;
int *pointer = &x;

cout << *pointer << endl;

*/
using namespace std;

void Load()
{

}

void Save()
{
    ofstream File_In;


}

void Game()
{

}

int main()
{
    int choice = 0;

    Player PO;


    cout << "Warehouse Manager\n" << endl;

    cout << "1) New Game" << endl;
    cout << "2) Load Game" << endl;
    cin >> choice;

    if(choice == 1)
    {
        cout << "Welcome, what is your name?" << endl;
        getline(cin, PO.get_Name());

        cout << "/nOk " << PO.get_Name() << " Lets get some info before we start\n" << endl;
        cout << PO.get_Money() << endl;
    }
    if(choice == 2)
    {
        Game();
    }
}



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
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED

#include <string>

using namespace std;

class Player
{
    public:
        Player();
        void Save();
        void Load();
        void Game();

        void set_Money(int M);
        void set_Name(string N);

        int get_Money();
        string get_Name();

    private:
        int money;
        string Player_Name;
};

Player::Player(): money(15000), Player_Name("")
{

}

void Player::set_Money(int M)
{
    money = M;
}

void Player::set_Name(string N)
{
    Player_Name = N;
}


int Player::get_Money()
{
    return money;
}

string Player::get_Name()
{
    return Player_Name;
}

#endif // PLAYER_H_INCLUDED
- Why would you try to access a private member variable without using one of your methods? Seriously?

- Moreover, doing so defeats the purpose of having a private member variable, which is why your code didn't work.
Like i said i havent done this in a while so i need to be remnded i need to see code because im a visual p[erson i cant just be told to do this and that, it is 110% impossible for me to do anything without seeing code.
Ch1156 wrote:
Like i said i havent done this in a while so i need to be remnded i need to see code because im a visual p[erson i cant just be told to do this and that, it is 110% impossible for me to do anything without seeing code.

- Do you have a book or do you only use Online resources?
I watch video tutorials online.
Ch1156 wrote:
I watch video tutorials online.


- I would personally advise you to invest in a book, but if you're not going to do that i suggest that you watch some of those tutorials again.
Topic archived. No new replies allowed.