I’m trying to compile this C++ code:
#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 ';'"
And I can’t understand why, because #define doesn’t need ';' at the end.
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
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.)
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.As @JoachimIsaksson has noted, a needed semicolon may be missing from the end of the file
general_configuration.hor the filehelper_function.h. You should check the last line in each file.