0

I have a Code::Blocks project with a few files, including main.cpp, Line_segment.cppand its corresponding header, Line_segment.h. The header file, as well as the main file, #include the <cmath> library in their preprocessor directives. One of the objects defined in <cmath> is the constant M_PI, which is simply the number pi.

Using M_PI works just fine in main(), and I'm able to write simple things like std::cout << M_PI*2. << '\n' and I'll get an output of 6.28... as expected. However, when I try to do the same thing in the Line_segment.cpp file and compile, I get a compilation error saying that 'M_PI' was not declared in this scope. The same happens if I include <cmath> directly into the .cpp file, and if I try using "math.h". Another observation is that M_PI, M_PI_2 and M_PI_4 all show up in the code suggestion popup when I type the name out, which implies that Code::Blocks is recognizing the name in the scope of that file.

Is there any known reason why this happens? Any help is greatly appreciated.

Johan
  • 343
  • 1
  • 4
  • 13
  • 6
    A [mcve] goes a long way. – chris Jun 25 '16 at 21:41
  • ... ^ and never had gone to be happening actually otherwise- – πάντα ῥεῖ Jun 25 '16 at 21:45
  • `M_PI` is *de facto* standard, but it's not standard in the sense of being defined by the ISO C++ standard (the *de jure* standard). And so its definition is probably protected by some preprocessor symbol. See http://stackoverflow.com/questions/26065359/m-pi-flagged-as-undeclared-identifier for info relevant to g++ and MSVC. – Cheers and hth. - Alf Jun 25 '16 at 22:18
  • My own theory is that in 2035, when use of C++ has declined to almost nothing, the standardization committee will hit on the idea of standardizing *existing practice*, instead of inventing new stuff, and then `M_PI` may stand a chance of being standardized. However, they'll probably change the name to something less obvious. – Cheers and hth. - Alf Jun 25 '16 at 22:21
  • After some experimentation, I found that the M_PI is defined when I `#include "SDL.h"`, the graphics library. SDL was included in my `main` file but not tin the `line_segment` file. Any ideas why this happens? – Johan Jun 25 '16 at 22:29

0 Answers0