Problem with classes....

I keep getting undeclared identifiers when I try to apply a class, I'm pretty sure I'm doing something wrong, just not sure what.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> //input/output
#include <string> //strings
#include "header1.h" //header1

int main()
{
cout << "Welcome please enter your name." << endl;
cin >> name >> endl;
//undeclared identifier
player::setName(player1);
//undeclared identifier
}


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
#ifndef HEADER1_H // if Person class is not defined

#define HEADER1_H // then define (multiple definitions will cause problems)

#include <iostream>
#include <string>

using namespace std;

class player
{
private:
string name;
string player1;//variable to hold players name
int win;//variable to hold win count
int loss;//variable to hold loss count
public:
void print() const;//function to print players name
void setName(string player1);//function to set player names
string getName() const;//function to get names
void winloss() const;//function to print win/loss
player();//default constructor
};

#endif // end the compiler directive for the define 


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


void player::print() const//function to print name
{
    cout << player1 << endl;
}
void player::setName(string player1)//function to set name
{
    name = player1;
}
string player::getName() const//function to get name
{
    return player1;
}
void player::winloss() const//function to print win/loss
{
    cout << win << " " << loss << endl;
}
player::player()//default constructor
{
    win = 0;
    loss = 0;
    player1 = " ";
    name = " ";
}
you're not using namespace std; or using std::stringin the implementation
you cant do this:
player::setName(player1);


make an object of player first.
1
2
player p;
p.setName(player1);


second thing, player1 is not declared, declare this variable first.
Thanks I got it now, I just misread the chapter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> //input/output
#include <string> //strings
#include "header1.h" //header1

using namespace std;

int main()
{
cout << "Welcome please enter your name." << endl;
string name;
player playerType;
cin >> name;
playerType.setName(name);

playerType.print();

}


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
#ifndef HEADER1_H // if Person class is not defined

#define HEADER1_H // then define (multiple definitions will cause problems)

#include <iostream>
#include <string>

using namespace std;

class player
{
private:
string playName;
string player1;//variable to hold players name
int win;//variable to hold win count
int loss;//variable to hold loss count
public:
void print() const;//function to print players name
void setName(string player1);//function to set player names
string getName() const;//function to get names
void winloss() const;//function to print win/loss
void playerName(string playName);
player();//default constructor
};

#endif // end the compiler directive for the define 


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

using namespace std;

void player::print() const//function to print name
{
    cout << "Player name is " << playName;
}
void player::setName(string player1)//function to set name
{
    playName = player1;
}
string player::getName() const//function to get name
{
    return player1;
}
void player::winloss() const//function to print win/loss
{
    cout << win << " " << loss << endl;
}
void player::playerName(string playName)
{
    string player1 = playName;
}
player::player()//default constructor
{
    win = 0;
    loss = 0;
    player1 = " ";
    playName = " ";
}
Topic archived. No new replies allowed.