2014-12-03 54 views
0

我有3个类:二叉搜索树,字典和联系人列表。字典使用二叉树,现在我想让联系人列表使用字典。问题是,与树和字典类不同,字典和联系人类使用不同数量的参数。这意味着我可以在字典类中使用wiktionary.find(),如代码中所标记的,我无法对联系人列表类中的wontact.find()也做同样的处理,这也是标记的。任何人都可以帮助我弄清楚如何使用联系人列表类中的字典功能?其他类中的类函数 - 二叉树,词典,联系人列表(C++)

词典(这个工程,并从树使用功能)

#include "bst.h" 



template <class K, class V> class Dictionary 
{ 
public: 

    BinarySearchTree<K,V> wiktionary; //this is what I can't do in contact list 

    Dictionary() 
    { 
    } 
    ~Dictionary() 
    { 
    } 

    V & find (K key) 
    { 
     wiktionary.find(key);  //uses find function in tree 
     return wiktionary.find(key); 
    } 

V & operator[] (K key) 
{ 
    try 
    { 
    find(key); 
    }catch(key_not_found_exception) 
    { 
     insert(key, 0); 
    } 

    return find (key); 
} 


private: 

}; 

和联系人列表:

#include "dictionary.h" 

class ContactList 
{ 
public: 
    ContactList() 
    { 
    } 
    ~ContactList() 
    { 
    } 

    Dictionary<K,V> wontact; //This is what I'm trying to achieve, 
    //but K and V are invalid according to compiler 



    /* 
    * Looks for an entry in contacts that matches name and 
    * returns the phone number of that entry 

    * Returns: 
    * if found string representing contacts phone number 
    * else returns an empty string 
    */ 

    string lookup (string name) 
    { 
     wontact.find(name); //this is what I'd like to do 


     //Work in progress 

     else 
     return ""; 
    } 


private: 

}; 
+0

有了模板,你的'K'和'V'在'Dictionary'是*占位符*的时候你使用这个类。假设“关键”和“价值”应该是什么类型......?也许是'std :: string'? – crashmstr 2014-12-03 20:17:51

+0

是的,这两个都是字符串 – Mock 2014-12-03 20:28:59

+0

然后在'Dictionary wontact;'用'std :: string'替换'K'和'V'(就像你为'std :: vector '所做的那样)。 – crashmstr 2014-12-03 20:30:08

回答

0

对于使用C++模板类,你需要提供你想要的实际类型以使用(例如:std::vector<std::string>)。

在这种情况下,重点和std::string价值,你会宣称你的类成员为:

Dictionary<std::string, std::string> wontact;