C++- 错误 C2144 语法错误:'int' 前面应该有 ';'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11808432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:33:46  来源:igfitidea点击:

C++- error C2144 syntax error : 'int' should be preceded by ';'

c++

提问by judith

I'm trying to compile this C++ code:

我正在尝试编译此 C++ 代码:

#include <stdlib.h>
#include <stdio.h>   
#include <string.h>
#include "general_configuration.h"
#include "helper_functions.h"

#define LINE_LEN 80

// file_with_as_ext returns 1 if the input has .as extension
int file_with_as_ext(char* input)
{
  char* dot_value = strchr(input, '.');
  if (dot_value == NULL)
    return 0;
  else
  {
    if (strcmp(dot_value,".as") == 0)
      return 1;
  }
}

But I'm getting the error "C2144: syntax error : 'int' should be preceded by ';'"

但我收到错误 "C2144: syntax error : 'int' should be preceded by ';'"

And I can't understand why, because #definedoesn't need ';'at the end.

我不明白为什么,因为最后#define不需要';'

回答by thb

First, the code you have posted begins with a stray backtick. If that's really in your code, you should remove it.

首先,您发布的代码以一个杂散的反引号开头。如果这确实在您的代码中,您应该将其删除。

Second, the compiler would be happier, and emit fewer warnings, if you ended your function with the line

其次,编译器会更快乐,并且发出更少的警告,如果你用以下行结束你的函数

return 0; // unreachable

This is good C++ style and is recommended. (In your case, the line may actually be reachable,in which case the line is not only good style but necessary for correct operation. Check this.)

这是很好的 C++ 风格,值得推荐。(在您的情况下,该线路实际上可能是可达的,在这种情况下,该线路不仅是好的样式,而且是正确操作所必需的。检查这个。)

Otherwise, your code looks all right except for some small objections one could raise regarding the outdated, C-style use of #defineand regarding one or two other minor points of style. Regarding the #define, it is not C++ source code as such but is a preprocessor directive.It is actually handled by a different program than the compiler, and is removed and replaced by proper C++ code before the compiler sees it. The preprocessor is not interested in semicolons. This is why the #defineline does not end in a semicolon. Neither do other lines that begin #usually end in semicolons.

否则,您的代码看起来没什么问题,除了一些关于过时的 C 风格使用#define以及其他一两个小风格点可能会提出的小反对意见。关于#define,它不是 C++ 源代码,而是一个预处理器指令。它实际上由与编译器不同的程序处理,并在编译器看到它之前被删除并由适当的 C++ 代码替换。预处理器对分号不感兴趣。这就是该#define行不以分号结尾的原因。其他#以分号开头的行通常也不以分号结尾。

As @JoachimIsaksson has noted, a needed semicolon may be missing from the end of the file general_configuration.hor the file helper_function.h. You should check the last line in each file.

正如@JoachimIsaksson 所指出的,文件general_configuration.h或文件末尾可能缺少所需的分号helper_function.h。您应该检查每个文件中的最后一行。

回答by Serval

I encounted this issue. I wrote a header file, but I forgot to add ";" at the tail of a function declaration. So, there is a error in my c file which is include this header file. I add comment here, and hope it will be useful for someone.

我遇到了这个问题。我写了一个头文件,但是我忘了加“;” 在函数声明的尾部。因此,我的 c 文件中有一个错误,其中包含此头文件。我在这里添加评论,希望它对某人有用。

回答by fredoverflow

And I can't understand why, #define doesn't need ';' at the end.

我不明白为什么,#define 不需要 ';' 在末尾。

Because #define is not a statement, but a preprocessor directive, and the preprocessor is line-oriented when it comes to separating directives. For example, you cannot put two #defines on the same line.

因为#define 不是语句,而是预处理器指令,并且预处理器在分离指令时是面向行的。例如,您不能将两个 #define 放在同一行上。