[英]Must return value, Function Overloading
目前,我正在嘗試運行一個程序,您可以選擇一個整數或十進制數,並根據該數字進行操作。 我的教授告訴我使用函數重載來使它起作用。 但是,每次我嘗試編譯它時,它都會給我101條警告和2條錯誤: 'pick' must return a value
。 我沒有正確聲明嗎? 我遵循了教科書中的模板,並在網上找到了一些模板,但無法正常使用。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//Declarations
int userInt;
double userFloat;
char selection, ans;
static double counterInt, counterFloat;
//Prototypes
int pick(int);
double pick(double);
//Defaults
counterInt = 0;
counterFloat = 0;
ans = 'Y';
//Game
do
{
cout << "Please choose an integer or a decimal number by typing in i or d" << endl;
cin >> selection;
if(selection == 'i')
{
cout << "Please enter an integer" << endl;
cin >> userInt;
cout << pick(userInt);
counterInt++;}
else
{
cout << "Please enter a decimal" << endl;
cin >> userFloat;
cout << pick(userFloat);
counterFloat++;}
//Play Again?
cout << "Would you like to play again? Please enter Y or N" << endl;
cin >> ans;
}
while ((ans == 'Y') || (ans == 'y'));
//End Game
if ((ans != 'Y') && (ans != 'y'))
{
cout << "Thank you for playing! You picked integers: " << counterInt << " times and you picked decimals: " << counterFloat << " times." << endl;
}
return 0;
}
//Function Overload
int pick(int number)
{
if( number % 7 == 0)
{
cout << setw(7) << "7777777" << setw(7) << number << endl;
}
else
{
cout << setw(7) << "*******" << setw(7) << number << endl;
}
}
double pick(double number)
{
cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << pow(number,.5) << endl;
}
您的兩個pick
函數都分別聲明為返回int
或double
,但它們都不具有return語句。
//Function Overload
int pick(int number)
{
if( number % 7 == 0)
{
cout << setw(7) << "7777777" << setw(7) << number << endl;
}
else
{
cout << setw(7) << "*******" << setw(7) << number << endl;
}
return number; // <<<<<<<<<<<<
}
double pick(double number)
{
double sqrt = pow(number,.5);
cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << sqrt << endl;
return sqrt; // <<<<<<<<<<<<
}
或者,如果他們實際上不需要返回任何東西,只需將返回類型更改為void
:
void pick (int number) { ... }
void pick (double number) { ... }
您有一個名為pick的函數,並且告訴編譯器該函數完成算法后將返回整數。 在函數中,您從不返回任何內容,只需編寫一個保存在var號中的特定值。 您要么必須將函數設為void(這意味着沒有返回值),要么必須返回任何值,例如,“ 1”表示成功,“-1”表示失敗。
//Function Overload
int pick(int number)
{
if( number % 7 == 0)
{
cout << setw(7) << "7777777" << setw(7) << number << endl;
return(1)
}
else
{
cout << setw(7) << "*******" << setw(7) << number << endl;
return(-1)
}
}
如果您不想返回任何值或變量,也可以使用void
//Function Overload
void pick(int number)
{
if( number % 7 == 0)
{
cout << setw(7) << "7777777" << setw(7) << number << endl;
}
else
{
cout << setw(7) << "*******" << setw(7) << number << endl;
}
}
因此,每次在函數之前編寫類型(讓它為class,int,double,bool ..)時,都必須返回適合規范的值。 類型
當將函數定義為int
作為其返回類型時,該函數必須返回int
。 否則,在調用該函數時,程序可能會具有未定義的行為。
您可以通過兩種方式解決問題:
更新函數以返回值。
int pick(int number) { if( number % 7 == 0) { cout << setw(7) << "7777777" << setw(7) << number << endl; } else { cout << setw(7) << "*******" << setw(7) << number << endl; } return 0; // Or something that makes more sense in your program. }
更新函數以返回void
。
void pick(int number) { if( number % 7 == 0) { cout << setw(7) << "7777777" << setw(7) << number << endl; } else { cout << setw(7) << "*******" << setw(7) << number << endl; } }
您可以對其他重載函數進行類似的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.