C ++ - syntax error C2144 errors: 'int' must precede ';'

I am trying to compile this code in 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 get the error message "C2144: syntax error : 'int' should be preceded by ';'" And I can not understand why, #define does not need ';' in the end.

any ides?

+6
source share
3 answers

First, the code you posted starts with a wandering backward stroke. If this is valid in your code, you should remove it.

Secondly, the compiler will be happier and issue less warnings if you end your function with a string

 return 0; // unreachable 

This is a good C ++ style and is recommended. (In your case, the line can really be reached, and in this case the line is not only good, but also necessary for proper operation. Check this out.)

Otherwise, your code looks correct, with the exception of some minor objections that could be raised regarding the obsolete use of the C-style #define style and with respect to one or two other minor style points. As for #define , this is not C ++ source code per se, but a preprocessor directive. It is actually processed by a program other than the compiler, and is removed and replaced with the appropriate C ++ code before the compiler sees it. The preprocessor is not interested in a semicolon. This is why the #define line does not end with a semicolon. Other lines starting with # usually end with a semicolon.

As @JoachimIsaksson noted, the desired semicolon may not helper_function.h at the end of the general_configuration.h file or the helper_function.h file. You should check the last line in each file.

+9
source

I ran into this problem. I wrote a header file, but I forgot to add ";" at the tail of the division function. So, in my c file there is an error that includes this header file. I am adding a comment here and hope it will be useful for someone.

+5
source

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

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

+1
source

Source: https://habr.com/ru/post/922113/


All Articles