错误 C2143:语法错误:'if'之前缺少';'
error C2143: syntax error : missing ';' before 'if'
#include <iostream>
using namespace std;
int factor(int n);
int main()
{
int f,n;
// Get user input
cout << "Enter an integer: ";
cin >> n;
// Call factorial function
f = factor(n);
// Output results
cout << n << "! = " << f << endl;
int factor (int n)
if(n <=1)
{
return 1;
}
else
{
int c = n * (n-1);
return c;
}
};
我收到错误 C2143:语法错误:在"if"之前缺少";"我很好奇我是否错过了一些简单的东西。我对C++相当陌生。
您正在尝试在函数main
中定义函数factor
。这在C++是不允许的。此外,factor
的功能体需要大括号:
int factor(int n)
{
// function body
}
int main()
{
// function body, factor visible
}
您需要将
因子函数从主函数中取出,并将代码放入手镯中。
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 错误:无效的预处理指令 #i 的意思是 #if?
- 为什么切换 if else 语句的顺序会导致错误?
- 为什么 if 语句在 Java 中"if(1)"给出错误,但在C++中成立?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 在if constexpr中使用带参数包的概念时,升级到gcc 9后出现编译错误
- 编译器错误"在if语句中分配不兼容的类型"
- C++ if 语句错误
- 否则在新程序中没有以前的 if 错误
- 对'if constexpr'的错误理解
- C++带有 while 语句范围捕获的 if 语句中的错误
- 将子字符串与字符串数组进行比较的 IF 条件会在C++中产生错误
- c++ IF 语句执行,但添加 ELSE 会显示错误
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- C++ 带有 if 语句的家庭作业编译错误
- 为什么使用 and 运算符比较 if 语句中的 2 个对象会抛出错误,而使用 2 if 语句则不会
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- SDL 应用程序错误:“if”之前应为非限定 id
- 第 48 行和第 62 行 C++错误 if之前的预期非限定 id