Reputation: 65
This is my program which leads to compilation errors mentioned below:
#include <iostream>
using namespace std;
class weight{
private:
float current_weight, diet_weight, num_exercise;
int num_meal,i;
public:
// make users to enter current weight
void get_curent_weight()
{
cout<<"Please enter your current weight: ";
cin>>current_weight;
}
void enter_data()
{
for (i=1; i>7; i++)
{
cout<<"Day "<<i<<endl;
cout<<"--------------"<<endl;
// Number of Meal(s)
cout<<"Number of meal(s) you eat today: ";
cin>>num_meal;
cout<<endl;
// Number of Exercise
cout<<"Number of hour(s) you spent on exercises today: ";
cin>>num_exercise;
cout<<endl;
}
}
// output the information
void information()
{
// calculate the final weight
diet_weight=current_weight+(0.5*num_meal)-(num_exercise/3)*0.5;
cout<<"Your weight after 7 days diet: "<<diet_weight<<endl;
}
};
int main(){
get_curent_weight();
enter_data();
information();
return(0);
}
I get the following compilation errors
error: 'get_curent_weight' was not declared in this scope
error: 'enter_data' was not declared in this scope
error: 'information' was not declared in this scope
It seems I call the function in wrong way.... Thank you for reading my question, as a beginner of C++, I appreciate it. :))
Upvotes: 1
Views: 60
Reputation: 2791
A class can be called a blueprint; it's functions can only be implemented on an object of the class that function belongs to. That function cannot be called as a standalone. To resolve your error, you need to declare an object in your main() function with the data type weight
and then call the function with that created object. An example of such would be like:
weight object_name;
object_name.get_curent_weight();
object_name.enter_data();
object_name.information();
Alternatively, you can take the functions out of the class; this way, they can be used without an object of class weight
. This will take a lot more effort over the first choice I suggested.
Upvotes: 0
Reputation: 42828
You need to create an instance of the class, and call the member functions on that instance:
int main() {
weight my_weight;
my_weight.get_curent_weight();
my_weight.enter_data();
my_weight.information();
return 0;
}
Upvotes: 1