I am using Visual Studio 2010 Express and I am getting the following errors for the file test.h, which when compiled outputs:
test.h(4): error C2061: syntax error : identifier 'test'
test.h(4): error C2059: syntax error : ';'
test.h(4): error C2449: found '{' at file scope (missing function header?)
test.h(18): error C2059: syntax error : '}'
The file test.h is described as follows:
#ifndef TEST_H
#define TEST_H
class test {
int a;
int b;
public:
test(int a, int b) {
this->a = a;
this->b = b;
}
int add() {
return 0;
}
};
#endif
The other file in the VS2010 project is test.c which is:
#include "test.h"
int main(int argc, char** argv) {
return 0;
}
I have a tried of multitude of ways to resolve this problem. Even if I define test.h as follows:
class test{
};
I still receive the same set of errors.
I saw a similar problem
https://stackoverflow.com/questions/7798876/strange-errors-when-using-byte-pbyte-instead-of-char-char-in-vs2k10-wdk-envi
with no response.
I will be really grateful if someone could please point out how to resolve these errors.
Thanks,
The Microsoft compiler supports both C and C++ languages, but they are not the same and need to be treated differently (for example
classis no keyword in C and thus ultimately causes the error your get). So it has to somehow “know” what kind of language (C or C++) it is dealing with when compiling a source file (and thus also processing the includes).It thinks you are trying to compile a C language file (because it has the file extension
.c), while you are actually using the C++ language. Rename your file to have one of the file extensions the Microsoft C/C++ compiler recognizes as C++:.cpp,.cxxor.cc.Alternatively, if you cannot rename the file, you can also use the
/Tpcommand line option ofcl.exeto force it to treat the file as a C++ file (for completeness/Tcwould force the C language).