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

jud*_*ith 6 c++

我正在尝试编译这个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;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误"C2144: syntax error : 'int' should be preceded by ';'" 我无法理解为什么,#define不需要';' 在末尾.

任何想法?

thb*_*thb 9

首先,您发布的代码以杂散的反引号开头.如果那真的在你的代码中,你应该删除它.

其次,如果您使用该行结束了您的功能,编译器会更快乐并发出更少的警告

return 0; // unreachable
Run Code Online (Sandbox Code Playgroud)

这是很好的C++风格,值得推荐.(在你的情况下,这条线实际上是可以到达的,在这种情况下,这条线不仅是好的风格,而且是正确操作所必需的.检查一下.)

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

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


小智 7

我列举了这个问题.我写了一个头文件,但我忘了添加";" 在功能贬值的尾部.所以,我的c文件中有一个错误,包括这个头文件.我在这里添加评论,并希望它对某人有用.