2013-12-09 39 views
1

我正在寫一個非平凡的類,它包含鍵值對的集合,並且在編譯期間收到一個非常奇怪的錯誤,我無法弄清楚。在功能,非常類似於在這裏這個功能,但是沒有由於所需的代碼的複雜背景下,我收到錯誤:'xyz'未聲明錯誤行'xyz'正在聲明

TValue& operator[](const TKey& key) { 
    TDict::Node* node = mData.Begin(); // ERROR: 'node' was not declared in this scope 
             // -_- ... really? 
    do { 
     if(node->Data.Key == key) { 
     return node->Data.Value; 
     } 
    } while(node != mData.End()); 

    this->Add(key, TValue()); 
    return this->End()->Data.Value; 
} 
  • TDict是一個擴展到List<KeyValuePair<TKey, TValue> >
  • TDict一個typedef ::節點在編譯過程中可見。
  • 顯然沒有其他變量被稱爲節點。
  • 這是一個成員函數。

我不是要求糾正這個問題的代碼,而是一個可能發生這種錯誤的潛在情況的概要。

+1

'TDict'是typedef模板類?請給出定義。 – ForEveR

+0

是的。它擴展到列表,其中TKVPair是一個擴展到KeyValuePair 的typedef。 –

回答

3

Its expands into List, where TKVPair is a typedef that expands into KeyValuePair.

它依存的名字,所以,你應該使用typename

typename TDict::Node* node = mData.Begin(); 

的更多信息,請閱讀Where and why do I have to put the "template" and "typename" keywords?

+0

謝謝。 GCC應該移動直觀的錯誤消息。我以前見過這個,但不記得如何解決它。再次謝謝你! –