2013-01-21 50 views
0
class MtmMap { 

    public: 

    class Pair { 

     public: 

     Pair(const KeyType& key, const ValueType& value) : 

      first(key), 

      second(value) { } 

     const KeyType first; 

     ValueType second; 

    }; 

    class node { 

     friend class MtmMap; 

     Pair data; 

     node* next; 

     public: 

     node(); 

     node(const Pair& pair){ 
      data.Pair(pair.first , pair.second); 
      next = NULL; 
     } 

    }; 

    node* temp = new node(pair); 

} 

錯誤:在地圖中的使用節點,C++

呼叫爲 'MTM :: MtmMap ::對::對()'

非法使用的「MTM :: MtmMap沒有匹配功能::對::對 '

從需要' 無效MTM :: MtmMap ::插入(常量

MTM :: MtmMap ::對&)[用關鍵字類型= INT; ValueType = int;

的compareFunction = AbsCompare]」

+0

你顯然沒有向我們展示你的所有代碼。 MtmMap大概是一個模板,是嗎?但是你沒有向我們展示模板。此外,它可以幫助指出錯誤發生在哪條線上。 –

回答

0

通過定義Pair這需要參數刪除隱含的默認構造函數不帶參數的構造函數。當您在nodePair類型的成員,因此它必須有傳遞給它的節點初始化器列表參數,需要用它來取代您的節點構造:

node(const Pair& pair) : data(pair.first, pair.second) { 
     next = NULL; 
    } 

這將調用構造函數data正常。 Learn Cpp有關於您可能想要閱讀的初始化程序列表工作方式的教程。

0

,在跳出我的第一件事是這一行:

node* temp = new node(pair); 

它看起來像你一個類定義中試圖new類。這可能是問題的一部分。此外,這個區域看起來有點困惑:

node(const Pair& pair){ 
    data.Pair(pair.first , pair.second); 
    next = NULL; 
} 

它看起來像你想直接調用構造函數Pair。我不確定這是否是有效的C++或不。真的,只是定義一個拷貝構造函數Pair像這樣:

Pair(Pair const& other) 
    : first(other.first) 
    , second(other.second) 
{} 

然後,該塊變成

node(Pair const& pair) 
    : data(pair) 
    , next(NULL) 
{} 

您還可以檢查到std::pair