0N3KN0WN 0N3KN0WN - 2 months ago 13
C++ Question

Cannot overload functions distinguished by return type alone C++

Hi I am working on a school project and have written all of this code from scratch (very simple I know). I am a bit confused, as I have solved all errors but two, and was wondering if someone could take a look over my source code and inform me of what I might be doing wrong, and how to solve it. Thanks!

Here are the errors I'm getting.

IntelliSense: cannot overload functions distinguished by return type alone
IntelliSense: no suitable conversion function from "std::string" to "double" exists

And my source code.

#include <iostream>
#include <string>
using namespace std;

//Global Constants
const double BMI_FACTOR = 703;
const double MASS_LOWER_LIMIT = 18.5;
const double MASS_UPPER_LIMIT = 25.0;

//Function Prototypes
double getWeight();
double getHeight();
double setMass(double, double);
string setOverUnder(double);
void showBMI(string, double);

//Begin Main
int main() {

//Variable Declaration
double weight;
double height;
double BMI; //Body Mass Index
string healthStatus; //Either Optimal, Under, or Over-weight

//Get the user's weight
weight = getWeight();

//Get the user's height
height = getHeight();

//Calculate the user's BMI
BMI = setMass(weight, height);

//Determine the user's health status based on BMI
healthStatus = setOverUnder(BMI);

//Displays the user's BMI and health status
showBMI(healthStatus, BMI);

return 0;

//End Main
}

//Function getWeight
double getWeight() {

//Local Variable Declaration
double totalWeight;

//User Input for Weight
cout << "Enter weight in pounds." << endl;
cin >> totalWeight;

//Return the value for totalWeight to caller
return totalWeight;

//End Function getWeight
}

//Function getHeight
double getHeight() {

//Local Variable Declaration
double totalHeight;

//User Input for Height
cout << "Enter height in inches." << endl;
cin >> totalHeight;

//Return the value for totalHeight to caller
return totalHeight;

//End Function getHeight
}

//Function setMass
double setMass(double localVarWeight, double localVarHeight) {

//Local Variable Declaration
double totalMass;

//Calculate the user's BMI
totalMass = (localVarWeight * BMI_FACTOR) / (localVarHeight * localVarHeight);

//Return the value of totalMass to the caller
return totalMass;

//End Function setMass
}

//Function setOverUnder
double setOverUnder(double localVarMass) {

//Local Variable Declaration
string wellBeing;

//Determine user's health
if (localVarMass < MASS_LOWER_LIMIT)
wellBeing = string("underweight.");
else if (localVarMass > MASS_UPPER_LIMIT)
wellBeing = string("overweight.");
else
wellBeing = string("optimal weight.");

//Return the value of wellBeing to the caller
return wellBeing;

//End Function setOverUnder
}

//Function showBMI
void showBMI(string localVarHealth, double localVarMass) {

//Display user's BMI
cout << "Your BMI is " << localVarMass << endl;

//Display user's health status
cout << "You are " << localVarHealth << endl;

//End Function showBMI
}

Answer

You have declared string as return type in function declaration but have used double instead of string in the definition. That's why it's not compiling.

//Function setOverUnder
string setOverUnder(double localVarMass) 
{

//Local Variable Declaration
string wellBeing;

//Determine user's health
if (localVarMass < MASS_LOWER_LIMIT)
    wellBeing = string("underweight.");
else if (localVarMass > MASS_UPPER_LIMIT)
    wellBeing = string("overweight.");
else
    wellBeing = string("optimal weight.");

//Return the value of wellBeing to the caller
return wellBeing;

//End Function setOverUnder
}
Comments