2016-10-02 22 views
0

嗨我正在學校項目,並從頭開始編寫所有這些代碼(我知道很簡單)。我有點困惑,因爲我已經解決了所有錯誤,但只有兩個錯誤,並且想知道是否有人可以查看我的源代碼,並告知我可能會做錯什麼,以及如何解決它。謝謝!不能重載由返回類型單獨區分的函數C++

這裏有錯誤

智能感知:不能重載返回類型單獨

智能感知區分功能:從「的std :: string」沒有合適的轉換功能,以「雙重」存在

和我的源代碼。

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

您是否期待每個人都猜測您在這裏傾倒的一堆代碼中的哪一行會產生此錯誤?如果你更具體地描述問題,人們可能會更容易幫助你。 –

回答

1

您已經在函數聲明中聲明瞭字符串作爲返回類型,但在定義中使用了double而不是字符串。這就是它沒有編譯的原因。

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

謝謝!這是多麼愚蠢的問題。 – 0N3KN0WN

1

改變setOverUnder定義返回一個字符串應該解決錯誤。

智能感知:不能重載通過單獨

由於setOverUnder函數聲明(返回字符串)不匹配其定義(返回雙)返回類型區分功能。

智能感知:從「的std :: string」沒有合適的轉換功能,以「雙重」存在

因爲你特林從函數返回setOverUnder返回雙串(健康)。

相關問題