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
//Test.cpp
//main.cpp
#include <iostream>
#include "Heartrate.h"
usingnamespace 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.
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.
// 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.
//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;
};
//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.
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>)}'
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?)
#include "Heartrate.h"
#include <iostream>
#include <string>
usingnamespace 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"
usingnamespace 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.
@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?