不能通过单独的返回类型C ++来区分重载函数

时间:2016-10-02 01:26:23

标签: c++

您好我正在开展一个学校项目并从头开始编写所有这些代码(我知道非常简单)。我有点困惑,因为我解决了所有错误而不是两个,并且想知道是否有人可以查看我的源代码并通知我可能做错了什么,以及如何解决它。谢谢!

以下是错误

  

IntelliSense:不能重载仅由返回类型区分的函数

     

IntelliSense:从“std :: string”到“double”不存在合适的转换函数

我的源代码。

#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
}

2 个答案:

答案 0 :(得分:1)

更改setOverUnder定义以返回字符串应解决错误。

  

IntelliSense:不能重载仅由返回类型区分的函数

因为setOverUnder函数声明(返回字符串)与其定义不匹配(返回double)。

  

IntelliSense:没有合适的转换函数来自&#34; std :: string&#34; to&#34; double&#34;存在

因为你要从返回double的函数setOverUnder返回一个字符串(wellBeing)。

答案 1 :(得分:1)

您已在函数声明中将字符串声明为返回类型,但在定义中使用了double而不是string。这就是为什么它没有编译。

//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
}