-1
#include <iostream>
using namespace std;

namespace characters {
    char tm='a';
    char tc='a';
}

using namespace characters;

class table {
    public:
        void printline (){
            char m;
            m=tm;
            //m=tc;
            cout<<m<<m<<m<<m<<m<<m<<m<<m<<m;
        }
};

int main()
{
    table myTable;
    myTable.printline();

return 0;
}

but when you comment out the m=tm; line and reinstate the line m=tc the code works fine.

what is so special about the identifier tm?

273K
  • 29,503
  • 10
  • 41
  • 64
  • 7
    `using namespace std;` is a bad practice and also is the cause of your bug. [https://en.cppreference.com/w/cpp/chrono/c/tm](https://en.cppreference.com/w/cpp/chrono/c/tm) – drescherjm Dec 29 '22 at 19:18
  • 1
    [A good compiler will point you to the error](https://godbolt.org/z/vEcM9Ev1K). – PaulMcKenzie Dec 29 '22 at 19:27
  • 2
    Tested a theory: https://godbolt.org/z/W9n3W8E4K Not a `using namespace std;` issue. At least not this time. Next time... There will be a next time. – user4581301 Dec 29 '22 at 19:27
  • thanks guys i think i now know why using the identifier tm caused the ambiguity – MultiBinary Dec 29 '22 at 19:30
  • 1
    @user4581301 its a `using namespace` issue ;) https://godbolt.org/z/aqWcoG5z4 – 463035818_is_not_an_ai Dec 29 '22 at 19:42
  • @user4581301 thanks! it has now come to my attention that using the identifier tm in a namespace conflicts with the global struct tm. – MultiBinary Dec 29 '22 at 19:47
  • @463035818_is_not_a_number Ah shoot. Should have looked at those diagnostics better. Well, at least it's a two-parter with the other `using` – user4581301 Dec 29 '22 at 20:00

1 Answers1

3

using namespace characters; is the issue, that brings characters::tm to the global namespace and makes the ambiguity with the global struct tm. The solution:

// using namespace characters;
using characters::tm;

That directs the compiler, if you meet tm, use here tm from the namespace characters.

273K
  • 29,503
  • 10
  • 41
  • 64