0

I am trying to set a global variable, but it is giving me errors:

"exp" is ambiguous

'exp': redefinition; previous definition was 'function'

I've searched this problem up but no one seems to be facing the same issue.

#include <string>
#include <windows.h>
using namespace std;

int level;
int exp;

void expBar()
{

    string bar = "--------------------";

    exp = 10;

    float progress = exp / bar.length();

    for (int i = 0; i < progress; i++)
    {
        bar[i] = '=';
    }

    cout << "[" << bar << "]";

}
  • 1
    https://stackoverflow.com/questions/11271889/global-variable-count-ambiguous – Steyrix Jul 23 '20 at 12:11
  • 1
    `std::exp()` exists and you bring it into the global namespace with `using namespace std;`. This, combined with fact that your `exp` is global variable makes the compiler confused. – Yksisarvinen Jul 23 '20 at 12:12
  • @Steyrix Good find, probably better than mine. – Yksisarvinen Jul 23 '20 at 12:12
  • exp stands for std::exp. The compiler thinks You created the variable exp with the same name as function std::exp. Do not write using namespace std;, where std::exp is defined. Instead of this write std::cout and std::endl; Or just rename Your variable: exp ->expvar, for example. – And Jul 23 '20 at 12:15
  • @Yksisarvinen Thanks dude, this worked I changed my variable from exp to xp. I didn't realise there was a pre-existing function called exp. – Harry Blackwood Jul 23 '20 at 12:15
  • Defining `exp` would be UB whether `using namespace std;` was used or not, because `exp` is a reserved name. – eerorika Jul 23 '20 at 12:17
  • @HarryBlackwood `I didn't realise there was a pre-existing function called exp` That's pretty much what the error message says :) – eerorika Jul 23 '20 at 12:18
  • @eerorika • A quick search... I can't find reference to `exp` being reserved. Where is that from? – Eljay Jul 23 '20 at 12:18
  • 1
    @Eljay To be more specific, it is reserved in the global namespace where it was defined in the example. *"Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace."*. – eerorika Jul 23 '20 at 12:20
  • @eerorika • Thanks! The global namespace is a minefield. – Eljay Jul 23 '20 at 12:22
  • @Eljay There is apparently even a better matching rule: *"Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage, or as a name of namespace scope in the global namespace."*. Which I suppose is subtly important. – eerorika Jul 23 '20 at 12:28

0 Answers0