-3

Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it.

The reason for the global variable is that I'm using multiple threads which need access to this variable and need to modify it.

My question is, I have int remainder = 0; declared globally, and within my master thread I call remainder = 13 % 5; for example.

This gives me an error saying 'int remainder' redeclared as a different kind of symbol and I've read that the reason is that using namespace std overrides the std::modulus operator, if I understood that correctly.

What other methods can I use to perform this function, keeping using namespace std and remainder as a global variable?

#include<iostream>
#include<cmath>

using namespace std;

int remainder = 0; 
void testing();

int main(){
    testing();
    cout << remainder << endl;
    return 0;
}

void testing(){
    remainder = 13 % 5;
}
  • 2
    Where did you read that? Also, please include as part of the question a [mcve] demonstrating the problem. – Phil M Mar 19 '19 at 22:41
  • Please be more specific than "gives me an error". (And post a [mcve] instead of a vague description of your code.) – molbdnilo Mar 19 '19 at 22:41
  • 2
    `std::modulus` is not pulled in unless you use `#include `, and even then it doesn't override the actual `%` operator anyway. `std::modulus` is a functor that uses `%` internally. It is useful for predicates and templates and such. What errors are you actually getting? They are related to something else. Please provide a [mcve] that demonstrates your error. – Remy Lebeau Mar 19 '19 at 22:42
  • 2
    Unrelated, but what are you doing to protect this global variable from race conditions? – Mike Borkland Mar 19 '19 at 22:43
  • And does this bit of code you've put in (that doesn't do any of the threading stuff) actually give you the same error (which error you'll need to include). – Phil M Mar 19 '19 at 23:10
  • Can't reproduce. The code you have shown does not produce the error you describe. – Peter Mar 19 '19 at 23:20
  • To protect global variables I am using semaphores and the threading portion is not what is throwing the error but rather it was simply the name of the variable itself as pointed out by Blastfurnace and Phil M. – JustWorkAlready Mar 19 '19 at 23:41

2 Answers2

4

The problem is your global variable name conflicts with std::remainder from the standard library. Example on Compiler Explorer.

The problem with using namespace std; is that it brings so many symbols into the global namespace that this error is almost inevitable. It's a bad practice for anything but the simplest toy programs.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Ahhh okay I see. The error I got was *remainder was redeclared as a different kind of symbol* and it was vague enough for me that I did not understand it was referring to the name of the variable itself. Thank you!! – JustWorkAlready Mar 19 '19 at 23:31
3

The conflict is with std::remainder, not with %. The variable name you've chosen conflicts with a function in the std namespace. You already know using namespace std; is bad, so I'll spare you.

Options:

  1. Lose the using statement.

  2. Rename the remainder variable.

  3. Put the remainder variable in its own namespace, and explicitly refer to it through that namespace.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Phil M
  • 1,619
  • 1
  • 8
  • 10
  • Actually `` almost everywhere pulls `remainder` into global namespace also, so not `using namespace std` won't solve the problem in this case. – yachoor Mar 19 '19 at 23:20
  • @yachoor Apparently doesn't do so on clang 7 (used by Repl.it), Have't yet found any on goldbolt that act as you describe, but then I've only been trying the most recent versions there. – Phil M Mar 19 '19 at 23:29
  • Yeah the solution seems to be renaming the variable itself as Blastfurnace and Phil M both pointed out. Thank you! – JustWorkAlready Mar 19 '19 at 23:32
  • @PhilM This is what I meant: https://gcc.godbolt.org/z/6a92fl - no using, still can't have remainder in global namespace (all extern names from C library are reserved in both std and global namespace) – yachoor Mar 19 '19 at 23:45
  • @yachoor Ah, yeah, you're correct. I apparently was testing with the wrong code. Typical, heh. – Phil M Mar 19 '19 at 23:58