Help with Classes

Hi, I'm really struggling with C++ and after doing some googling I've yet to figure out a way to fix the errors I'm getting. If someone could please help me figure out how to fix these I'd really appreciate it. This is my code so far:

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

#include "Jogger.h"

using namespace std;

int main() {

	string name;
	int walk;
	int jog;
	int run;

	cout << "Enter your name: ";
	cin >> name;
	cout << "Enter your miles walked: ";
	cin >> walk;
	cout << "Enter your miles jogged: ";
	cin >> jog;
	cout << "Enter your miles ran: ";
	cin >> run;

	Jogger Jogger_2;

	Jogger_2.setName(name);
	Jogger_2.setWalk(walk);
	Jogger_2.setJog(jog);
	Jogger_2.setRun(run);

	cout << endl << "Jogger Name: " << Jogger_2.getName() << endl <<
		"walked " << Jogger_2.getWalk() << "miles, " << endl <<
		"jogged " << Jogger_2.getJog() << "miles, and " << endl <<
		"ran " << Jogger_2.getRun() << "miles." << endl;


	return 0;
}


H File

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

using namespace std;

#ifndef JOGGER_H
#define JOGGER_H

class Jogger {
public:
	Jogger();

	Jogger(string, int);

	~Jogger();

	string getName() const;

	int getWalk() const;

	int getJog() const;

	int getRun() const;

	void setName(string);

	void setWalk(int);

	void setJog(int);

	void setRun(int);


private:
	string newName;
	int newWalk;
	int newRun;
	int newJog;
};


#endif


Second CPP (this is where I'm getting errors)

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

#include "Jogger.h"
Jogger::Jogger() {
	newWalk = 0;
	newRun = 0;
	newJog = 0;
}

Jogger::Jogger(string name, int walk, int jog, int run) {
	newName = name;
	newWalk = walk;
	newJog = jog;
	newRun = run;
}

Jogger::~Jogger() {

}

string Jogger::getName() const{
	return newName;
}

int Jogger::getWalk() const{
	return newWalk;
}

int Jogger::getJog() const{
	return newJog;
}

int Jogger::getRun() const{
	return newJog;
}

void Jogger::setName(string name) {
	newName = name;
}

void Jogger::setWalk(int) {
	newWalk = walk;
}

void Jogger::setJog(int) {
	newJog = jog;
}

void Jogger::setRun(int) {
	newRun = run;
}


Errors:
-Error 1 (Line 8) error C2511: 'Jogger::Jogger(std::string,int,int,int)' : overloaded member function not found in 'Jogger'
-Error 2 (Line 9) error C2597: illegal reference to non-static member 'Jogger::newName'
-Error 3 (Line 10) error C2597: illegal reference to non-static member 'Jogger::newWalk'
-Error 4 (Line 11) error C2597: illegal reference to non-static member 'Jogger::newJog'
-Error 5 (Line 12) error C2597: illegal reference to non-static member 'Jogger::newRun'
-Error 6 (Line 40) error C2065: 'walk' : undeclared identifier
-Error 7 (Line 44) error C2065: 'jog' : undeclared identifier
-Error 8 (line 48) error C2065: 'run' : undeclared identifier
-Error 9(Line 8) IntelliSense: no instance of overloaded function "Jogger::Jogger" matches the specified type
-Error 10 (Line 40) IntelliSense: identifier "walk" is undefined
-Error 11 (Line 44) IntelliSense: identifier "jog" is undefined
-Error 12 (Line 48) IntelliSense: identifier "run" is undefined
Last edited on
Sorry still have to learn how to make multiple files with code::blocks.
Indeed I don't know much about using a class although I get the basic idea.
Placed all your code in one file,
Used the compile errors to figure out what to change.
This appears to run ok?
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>

using namespace std;

class Jogger {
public:
	Jogger();

	Jogger(std::string, int);

        Jogger(string, int, int, int);

	~Jogger();

	string getName() const;

	int getWalk() const;

	int getJog() const;

	int getRun() const;

	void setName(string);

	void setWalk(int);

	void setJog(int);

	void setRun(int);


private:
	string newName;
	int newWalk;
	int newRun;
	int newJog;
};

Jogger::Jogger() {
	newWalk = 0;
	newRun = 0;
	newJog = 0;
}

Jogger::Jogger(string name, int walk, int jog, int run) {
	newName = name;
	newWalk = walk;
	newJog = jog;
	newRun = run;
}

Jogger::~Jogger() {

}

string Jogger::getName() const{
	return newName;
}

int Jogger::getWalk() const{
	return newWalk;
}

int Jogger::getJog() const{
	return newJog;
}

int Jogger::getRun() const{
	return newJog;
}

void Jogger::setName(string name) {
	newName = name;
}

void Jogger::setWalk(int walk) {
	newWalk = walk;
}

void Jogger::setJog(int jog) {
	newJog = jog;
}

void Jogger::setRun(int run) {
	newRun = run;
}

int main() {

	string name;
	int walk;
	int jog;
	int run;

	cout << "Enter your name: ";
	cin >> name;
	cout << "Enter your miles walked: ";
	cin >> walk;
	cout << "Enter your miles jogged: ";
	cin >> jog;
	cout << "Enter your miles ran: ";
	cin >> run;

	Jogger Jogger_2;

	Jogger_2.setName(name);
	Jogger_2.setWalk(walk);
	Jogger_2.setJog(jog);
	Jogger_2.setRun(run);

	cout << endl << "Jogger Name: " << Jogger_2.getName() << endl <<
		"walked " << Jogger_2.getWalk() << "miles, " << endl <<
		"jogged " << Jogger_2.getJog() << "miles, and " << endl <<
		"ran " << Jogger_2.getRun() << "miles." << endl;


	return 0;
}

Last edited on
I was scratching my head admittedly at the issue (beginner myself). I noticed something different between your one and the poster below you.

In your Jogger.cpp file you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Jogger::setName(string name) {
	newName = name;
}

void Jogger::setWalk(int) {
	newWalk = walk;
}

void Jogger::setJog(int) {
	newJog = jog;
}

void Jogger::setRun(int) {
	newRun = run;
}


Whereas the updated one has this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Jogger::setName(string name) {
	newName = name;
}

void Jogger::setWalk(int walk) {
	newWalk = walk;
}

void Jogger::setJog(int jog) {
	newJog = jog;
}

void Jogger::setRun(int run) {
	newRun = run;
}


This is why you never ran into an issue with name as you declared it. The other 3 int variables, however you did not. Take my words with a grain of salt however as I am just starting myself :)
Last edited on
In the class public declarations I also added,
Jogger(string, int, int, int);
Last edited on
I am also a beginner. But I remember this from something similar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Jogger::setName(string name) {
	newName = name;
}

void Jogger::setWalk(int walk) {
	newWalk = walk;
}

void Jogger::setJog(int jog) {
	newJog = jog;
}

void Jogger::setRun(int run) {
	newRun = run;
}


The parts you have added are called temporary variables or something. They are necessary and will be deleted after the functions have run.

Jogger(string, int, int, int);

You are declaring there will be these variables. In this one:

Jogger(string, int, int, int);

This is the overloaded constructor

Just to explain them...I believe it is called "pass by value".

I can upload a college report on Pass by data/reference/address but i'm not sure how on this site :P

To me it seems you have no problem with inheritance but are just a bit confused about passing variables down into functions
Last edited on
morning dude,
Your error1 is because you have this in your .cpp file:
1
2
3
4
5
6
Jogger::Jogger(string name, int walk, int jog, int run) {
	newName = name;
	newWalk = walk;
	newJog = jog;
	newRun = run;
}

but you have no corresponding signature in your class declaration. You simply need to add this:
 
Jogger(string name, int walk, int jog, int run);

into your header file. Perhaps line 13 of your header file was meant to be this, seeing as I can't find any constructor implementations in your cpp that just take a string and an int?
edit: I've just realised codewriter gave you the answer :)
Last edited on
Topic archived. No new replies allowed.