递归阶乘错误 return-带有值的语句,在函数 returning 'void' [-fpermissive] 中
Recursive factorial error return-statement with a value, in function returning 'void' [-fpermissive]
我尝试使用递归函数编写阶乘。这段代码有什么问题?
我在函数 returning 'void' [-fpermissive]
中收到带有值的错误 return 语句
#include <iostream.h>
int factorial(int);
int factorial(int number)
{
return number==0?1: number * factorial*(number - 1);
}
void main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}
函数 main
应该 return int
,而不是 void
。此外,括号内的 void
在 C++ 中是无用的。
使用:
int main() {
或:
auto main() -> int {
修复代码。
main 必须是 int 并且函数参数应该立即输入,中间没有 *
:
#include <iostream.h>
int factorial(int number)
{
return number==0?1: number * factorial(number - 1);
}
int main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}
我尝试使用递归函数编写阶乘。这段代码有什么问题? 我在函数 returning 'void' [-fpermissive]
中收到带有值的错误 return 语句#include <iostream.h>
int factorial(int);
int factorial(int number)
{
return number==0?1: number * factorial*(number - 1);
}
void main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}
函数 main
应该 return int
,而不是 void
。此外,括号内的 void
在 C++ 中是无用的。
使用:
int main() {
或:
auto main() -> int {
修复代码。
main 必须是 int 并且函数参数应该立即输入,中间没有 *
:
#include <iostream.h>
int factorial(int number)
{
return number==0?1: number * factorial(number - 1);
}
int main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}