Reputation: 13
This error comes only when im working in a C file (not CPP)
I came across a very weird error while working on some opengl sample, I narrowed it down to below code, which produces the same error I'm getting, type not found
#include "stdlib.h"
void Initialize(int, char*[]);
void main()
{
}
void Initialize(int argc, char* argv[])
{
int i;
for(i = 10; i>0;i--)
{
int j=0;
j = j+2;
int GlewInitResult2;
}
}
I used visual studio 2008, OS: windows xp
cant understand why this is happening? is this a compiler bug?
(btw, it compiles fine if i save the file as a .CPP file)
Upvotes: 1
Views: 182
Reputation: 2335
As far as i know Visual studio 2008 is not completely c99 compliant.
In standards before c99
you have to declare your variables in the beginning of the program. But in c99
declaration of variables can be done anywhere in the function.
Also in c++ there is no such restriction. In Windows when you create a file with extension .cpp
it is treated as a c++ file. Hence the same program works when compiled as cpp.
Solutions:
Upvotes: 2
Reputation: 399703
You have declarations after statements. In newer versions of C (like C99), you can have this but in older versions you cannot. Microsoft's C compiler is old. In C++ it's OK, which is why it builds.
Upvotes: 4
Reputation: 121961
The Microsoft compilers only support C89 so declarations and code cannot be mixed. This is permitted in C++. When the source file has a .c
extension it is treated as C source, when it has .cpp
it is treated as C++.
To correct for C put all variable declarations at beginning of the scope in which they are to be used.
Upvotes: 2