簡體   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;
}

但是當你注釋掉 m=tm; 行並恢復行 m=tc 代碼工作正常。

標識符 tm 有什么特別之處?

using namespace characters; 是將characters::tm帶入全局命名空間並與全局struct tm產生歧義的問題。 解決方案:

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

這會指示編譯器,如果遇到tm ,請在此處使用命名空間characters中的tm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM