Irbis
Irbis

Reputation: 1491

#ifdef and undeclared identifier

I develop a project in Visual Studio 2017. The following code:

#ifdef MY_DEFINE
#else
    std::string txt = "abc";
#endif

output += txt;

doesn't compile - I get an error: 'txt': undeclared identifier

MY_DEFINE is not defined and the else branch is compiled. That code is part of the bigger project. I can't reproduce that compilation error in the default simple console application. Does it mean that the project may have some setting which causes that compilation error ?

Upvotes: 0

Views: 616

Answers (1)

Arthur Tacca
Arthur Tacca

Reputation: 9978

You can check what's really happening using the #error directive:

#ifdef MY_DEFINE
#error It was defined
#else
#error It was NOT defined
    std::string txt = "abc";
#endif

output += txt;

It may be that MY_DEFINE is actually defined in some other header file you're including. Usually the easiest way to determine this in Visual Studio is just to hover your mouse cursor over the identifier and see what it says, or ctrl+click to go to the definition. But sometimes these hints in the IDE don't exactly match what happens you compile though.

Upvotes: 3

Related Questions