Classes, and Header declared void error

Getting three errors, two telling me I declared SetName and SetAge as void, and one telling me that my display() function is not a member function, and i cant figure out why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//What.h
//Heartrate.h
#include <string>
class Heartrate
{
	public:
	
	std::string PubName;
	int PubAge;
	
	Heartrate(int PubAge, std::string PubName);
	void display();
	
	void SetAge(int PubAge );
	int GetAge();
	
	void SetName(std::string PubName );
	std::string GetName();
	
	private:
	int PriAge;
	std::string PriName;
};	


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
//How.cpp
//Heartrate.cpp
#include "Heartrate.h"
#include <iostream>
#include <string>
using namespace std;
int PubAge;
string PubName;
Heartrate::Heartrate(int PubAge, string PubName)
{
	SetAge(PubAge);
	SetName(PubName);	
}

void Heartrate::display()
{
	cout << "Your name is" << GetName() << "And your age is " << GetAge() << endl;
}

void Heartrate::SetAge(PubAge)
{
	PriAge = PubAge;
}

int Heartrate::GetAge()
{
	return PriAge;
}

void Heartrate::SetName(PubName)
{
	PriName = PubName;
}

string Heartrate::GetName()
{
	return PriName;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Test.cpp
//main.cpp
#include <iostream>
 #include "Heartrate.h"
using namespace std;

int main()

{
	string PubName;
	int PubAge;
	int PriAge;
	Heartrate HR(int PubAge, string PubName);
	
	cout << "Welcome to the HeartRate Monitor, please enter your name" <<endl;
	getline(cin, PubName);
	cout << "Enter your age" << endl;
	cin >> PubAge;
	HR.display();
	
    //remember, this is how you call a member function, the DOT! and it is a function
    //so do not forget the () PARENTHESIES AND THE ; SEMICOLON
}
	


and my errors are
Heartrate.cpp:19:30: error: variable or field 'SetAge' declared void
Heartrate.cpp:29:32: error: variable or field 'SetName' declared void
Main.cpp: In function 'int main()':
Main.cpp:18:5: error: request for member 'display' in 'HR', which is of non-class type 'Heartrate(int, std::string) {aka Heartrate(int, std::basic_string<char>)}'

I realize I declared the functions void...but why is that an error? And as of when is my display not a member function of Heartrate? Any direction would be appreciated.
Last edited on
Well, on lines 10-13 you have 3 variables that are declared but unitialized, then you pass them to the constructor. After that you do input to them, but you don't do anything with those variables. Then you display the HR object. The other errors I'm not sure about but I'd imagine it has something to do with you variable naming since you have PugAge and PubName as members of the class as well as variables local to main(), not to mention you declare them in Heartrate.cpp as well. If you have setters and getters there really no need for public variables in the class.
Last edited on
I think This is the error:

1
2
3
4
5
// Heartrate.cpp, line 20
void Heartrate::SetAge(PubAge) // What is PubAge?
// Heartrate.cpp, line 30
void Heartrate::SetName(PubName) // What is PubName?

Add their type, like you are doing for the constructor.
Last edited on
so Raezzor, If i delete those lines, The main function cant figure out they are variables...and errors out.

EssGe, If i declare their types (void Heartrate::SetAge(int PubAge) i get an error telling me, its an incorrect call, when i call that function.
Compiler error or runtime error? Mind posting it?
Also:

1
2
3
Heartrate HR(int PubAge, string PubName);
// You should use the parameters there in a correct way like this:
Heartrate HR(24, "MyName");


You also need more knowledge about OOP and classes.
Last edited on
Lines 20 and 30 in heartrate.cpp are not function calls though, they are definitions. So you MUST include the parameter types.

What are you using to learn about classes if I may ask?
Try this once:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Heartrate.h
#include <string>
class Heartrate
{
	public:
		
	Heartrate(int age, std::string name); // c-tor
	
	void SetAge(int age);   // setters grouped together (just personal pref, your way was ok)
	void SetName(std::string name );

	int GetAge();                     //getters
	std::string GetName();

	void display();
	
	private:

	int hr_age;
	std::string hr_name;
};


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
//Heartrate.cpp
#include "Heartrate.h"
#include <iostream>
#include <string>

Heartrate::Heartrate(int age, std::string name)
{
	hr_age = age;
        hr_name = name;
}

void Heartrate::SetAge(int age)
{
	hr_age = age;
}

void Heartrate::SetName(std::string name)
{
	hr_name = name;
}

int Heartrate::GetAge()
{
	return hr_age;
}

std::string Heartrate::GetName()
{
	return hr_name;
}

void Heartrate::display()
{
	std::cout << "Your name is" << GetName() << "And your age is " << GetAge() << std::endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//main.cpp
#include <iostream>
#include <string>
#include "Heartrate.h"

int main()

{
	std::string inp_name;
	int inp_age;

	std::cout << "Welcome to the HeartRate Monitor, please enter your name" << std::endl;
	getline(std::cin, inp_name);
	std::cout << "Enter your age" << std::endl;
	std::cin >> inp_age;

	Heartrate HR(inp_age, inp_name); // you were using variables types in a function call, you only need the variable names

	HR.display();

        return 0;
	
 }


See how that works for you. There may be some small syntax bugs since I haven't actually tested it on my compiler.
Last edited on
Sorry for the delay, This is actually independent work to help me understand classes. What am I trying to learn about classes you say? Everything. I'm no good at them. At all. I just recently got the idea of having two .cpp files, and a .h file. So I'm really just running my head into a wall until i get it.
Original errors are

Heartrate.cpp:19:30: error: variable or field 'SetAge' declared void
Heartrate.cpp:29:32: error: variable or field 'SetName' declared void
Main.cpp: In function 'int main()':
Main.cpp:18:5: error: request for member 'display' in 'HR', which is of non-class type 'Heartrate(int, std::string) {aka Heartrate(int, std::basic_string<char>)}'
Also Raezzor why do you have this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Heartrate::Heartrate(int age, std::string name)
{
	hr_age = age;
        hr_name = name;
}

void Heartrate::SetAge(int age)
{
	hr_age = age;
}

void Heartrate::SetName(std::string name)
{
	hr_name = name;
}


it seems like its doing the same thing two times? I thought it would have to look something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Heartrate::Heartrate(int age, std::string name)
{
	SetAge(age);
        SetName(name);
}

void Heartrate::SetAge(int age)
{
	hr_age = age;
}

void Heartrate::SetName(std::string name)
{
	hr_name = name;
}


to call the member functions SetAge and SetName. If your code was correct, why not just have the constructor, and eliminate the setters and getters. (Unless you were just including them because I did, and I only did because I assumed that was common practice, to keep private variables and just use setters and getters to keep the private variables unaltered....or is that not the case?)
Also, I go it to work, but I'm not sure if this is the correct way to write this, or the most efficient? or in any way resembling correct code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Heartrate
{
	public:
	
	std::string PubName;
	int PubAge;
	
	Heartrate(int PubAge, std::string PubName);
	void display();
	
	void SetAge(int PubAge );
	int GetAge();
	
	void SetName(std::string PubName );
	std::string GetName();
	
	private:
	int PriAge;
	std::string PriName;
};


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
#include "Heartrate.h"
#include <iostream>
#include <string>
using namespace std;
int PubAge;
string PubName;
Heartrate::Heartrate(int PubAge, string PubName)
{

	//PriAge = PubAge;
	//PriName = PubName;
	SetAge(PubAge);
	SetName(PubName)
        //it will work with either one of these....Which one is correct? Are they both right, is one better then the other??	
}

void Heartrate::display()
{
	cout << "Your name is" << GetName() << "And your age is " << GetAge() << endl;
}

void Heartrate::SetAge(int PubAge)
{
	PriAge = PubAge;
}

int Heartrate::GetAge()
{
	return PriAge;
}

void Heartrate::SetName(std::string PubName)
{
	PriName = PubName;
}

string Heartrate::GetName()
{
	return PriName;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Heartrate.h"
using namespace std;

int main()

{
	string PubName;
	int PubAge;
	int PriAge;
	
	cout << "Welcome to the HeartRate Monitor, please enter your name" <<endl;
	getline(cin, PubName);
	cout << "Enter your age" << endl;
	cin >> PubAge;
	
	Heartrate HR( PubAge, PubName);
	HR.display();
}
If your code was correct, why not just have the constructor, and eliminate the setters and getters.


Constructors initialise member variables when the object is created, so you need to have other functions to change them at a later stage.

Try to avoid having a getter / setter for each member variable. Instead, think about how things might work in real life - have functions that set closely related things in 1 function. For example if you have a Person class, provide a setAddress function, which sets all the address variables.
You should not have public / private versions of the same variable. Make them private, then provide public functions to access them.
The GetName & Get Age functions are not needed because you have the display function. Remember class functions have direct access to member variables.

You would only need get functions if some other object needed access to these values.
Alright, thanks for the input. Like I say, this is just extra work, but Im glad you all are helping me. Merry Christmas
@TIM Well, the getters could still be useful if you need the values from the variables to do work on them outside the class other then just displaying them. Course, you could write that into the class as another member function too.

Now, as for your main() that you chose to use your original instead of my example, again you declare a bunch of the same named variables that you have in the class, and one that you don't even use. The first isn't an error, and for some reason some people don't see anything wrong with it, but from what I have read it is considered less then proper form and can lead to confusion and ambiguity. The int, PriAge, on line 10 of main() isn't even used, so why add it back in?

And, like TIM said, having public and private data members that contain the same information is pointless and wastes space and clock cycles. Setting the public data members then using setters to set the private members?

As for why you have constructors and setters in the same class, you don't really need to... unless the data the class contains might ever change. Sure, you could simply declare another object and get rid of the old one, but that could get cumbersome having to delete old objects and declaring new ones any time you make even the smallest change to your class.

Another thing I'm never all that keen on is calling setters in a constructor. I dunno why, it just feels... odd... to me. Prolly some good reasons why, or maybe some good reasons why I'm just being nutty, but eh. You can access the private members directly from the constructor, so why not do it and make it a little more efficient?

Last edited on
Topic archived. No new replies allowed.