Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why does the following code generate a "reference to 'tm' is ambiguous" error?

#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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading