简体   繁体   English

为什么以下代码会生成“对‘tm’的引用不明确”错误?

[英]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;但是当你注释掉 m=tm; line and reinstate the line m=tc the code works fine.行并恢复行 m=tc 代码工作正常。

what is so special about the identifier tm?标识符 tm 有什么特别之处?

using namespace characters; is the issue, that brings characters::tm to the global namespace and makes the ambiguity with the global struct tm .是将characters::tm带入全局命名空间并与全局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 .这会指示编译器,如果遇到tm ,请在此处使用命名空间characters中的tm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM