Tuesday, July 14, 2009

Compilation error and warnings in Visual C++ 6.0?

When I compile the following codes:





#include %26lt;iostream%26gt;


using namespace std;


#define pi 3.14;





void main ()


{





float radius(2),area;





area=pi*radius*radius;


}





I get the follow messages:


--------------------Configuration: Cpp1 - Win32 Debug--------------------


Compiling...


Cpp1.cpp


C:\VC++\Cpp1.cpp(12) : warning C4305: '=' : truncation from 'const double' to 'float'


C:\VC++\Cpp1.cpp(12) : error C2100: illegal indirection


C:\VC++\Cpp1.cpp(12) : warning C4552: '*' : operator has no effect; expected operator with side-effect


Error executing cl.exe.





Cpp1.exe - 1 error(s), 2 warning(s)





However, when I write the same thing but chage the position of 'pi' as follows, I don't get any error nor warning:





#include %26lt;iostream%26gt;


using namespace std;


#define pi 3.14;





void main ()


{





float radius(2),area;





area=radius*radius*pi; //position of pi changed here


}





How come? Please Help!

Compilation error and warnings in Visual C++ 6.0?
This is because you wrote


#define pi 3.14;


The preprocessor will replace any "pi" with "3.14;".


You just need to write


#define pi 3.14


without ";"


No comments:

Post a Comment