1

In Visual Studio 2012, I'm unable to declare certain names as global identifiers because they're already declared in math.h. Legacy issues makes it inconvenient for me to rename the identifiers in the source code. What are the options besides renaming?

#include "stdafx.h"
// iostream includes math.h which declares the following
_CRT_NONSTDC_DEPRECATE(_y1) _CRTIMP double  __cdecl y1(_In_ double _X);

int y1; // error - y1 is already declared

void Main()
{
    return;
}

Bonus question: Is Visual Studio 2012 handling this in a conforming manner?

BSalita
  • 8,420
  • 10
  • 51
  • 68
  • Are you sure `iostream` includes `math.h` and not `cmath`? Make sure you're not `using namespace std`. – obataku Sep 16 '12 at 00:49
  • iostream eventually gets cmath included which directly includes math.h. – BSalita Sep 16 '12 at 01:19
  • no, `cmath` is guaranteed to include `math.h` within the `std` namespace. – obataku Sep 16 '12 at 01:22
  • Indeed I am `using namespace std`. You've prompted me to read http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c. I can see now that I'm a misguided C programmer who didn't bother with the namespace lesson. Now corrected. – BSalita Sep 16 '12 at 02:12
  • would you like me to post an answer for you to accept it? – obataku Sep 16 '12 at 02:13

3 Answers3

2

Since this is C++, you should use a namespace for your own stuff, especially if you have global variables.

#include "stdafx.h"

namespace MyApp
{
    int y1; // MyApp::y1
}

This way, you can rely on the using keyword, where you need to use your y1 variable without the namespace name:

using MyApp::y1; // Now also y1
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • OP side note: are you sure `void Main()` is the proper signature here? Generally it's `int main` (MS's cl compiler might expect `int _tmain`). – obataku Sep 16 '12 at 00:51
  • 2
    `void Main()` is a perfectly valid function declaration (assuming a case-sensitive linker). It's just not the program's entry point. If it's *intended* to be the entry point, there are at least two things wrong with it. – Keith Thompson Sep 16 '12 at 01:01
  • 2
    @KeithThompson I'm clearly aware that `void Main()` is valid (... really?), but it doesn't take a genius to realize that he intended it as the entry point. – obataku Sep 16 '12 at 01:21
  • 1
    The entry point is indeed int _tmain which calls void Main. It would have been more canonical to use int _tmain. Using void Main caused distractions. Sorry. – BSalita Sep 16 '12 at 01:29
  • Thanks for the answer. As a C programmer now writing some C++ code, I had an enormous blindspot about namespaces. Now corrected. – BSalita Sep 16 '12 at 02:17
  • 1
    @oldrinb: As it turns out, he *didn't* intend `void Main()` to be the entry point. – Keith Thompson Sep 16 '12 at 02:22
  • `void Main()` was a wrong choice for the example but remains correct for the implementation. – BSalita Sep 16 '12 at 11:34
0

The identifier y1 may appear in some versions of <math.h>, but it's not defined by the C or C++ standard. You should be able to invoke the compiler in a way that avoids declaring y1, perhaps by disabling language extensions. If the compiler doesn't let you do this, that's a bug in the implementation.

y1() is one of the "Bessel functions of the second kind" (I'm not quite sure what that means). It's specified by POSIX.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

As I stated in my comments, iostream will not be including math.h, but rather cmath. The subtle difference is that cmath places everything in the std namespace. The problem is that you're doing something as follows...

using namespace std;

... don't. As you can see, you are yourself introducing an ambiguity in the global namespace. Instead, try to explicitly specify what you need from std, rather than polluting the global namespace with whatever std members have been declared in your included headers. You should also stray from using global variables and the global namespace itself.

As a side note, try to show whole code, since it was unclear that _tmain had called Main here.

obataku
  • 29,212
  • 3
  • 44
  • 57