Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 专用模板类能否从另一个专用模板类继承?_C++ - Fatal编程技术网

C++ 专用模板类能否从另一个专用模板类继承?

C++ 专用模板类能否从另一个专用模板类继承?,c++,C++,我首先定义 class Hash { }; 然后是散列的特殊化 template <class T> class Hash<int, T> { public: Hash(int slotN = 11); bool insert(int key, T val); bool remove(int key); bool contains(int key); bool query(int key, T& val) ; pro

我首先定义

class Hash
{

};
然后是散列的特殊化

template <class T>
class Hash<int, T>
{
public:
    Hash(int slotN = 11);

    bool insert(int key, T val);
    bool remove(int key);
    bool contains(int key);
    bool query(int key, T& val) ;

protected:
    // Basic Variables of the Hash Model.
    list<int>* slot;
    list<T>* slotVal;
    int slotN;
};
模板
类散列
{
公众:
散列(int-slotN=11);
布尔插入(int键,T值);
bool-remove(int键);
bool包含(int键);
布尔查询(int键、T&val);
受保护的:
//散列模型的基本变量。
列表*插槽;
列表*slotVal;
int slotN;
};
我想使用这个专门版本的散列 要实现另一个专门化:的哈希 字符串值键

template <class T>
class Hash<string, T> : public Hash<int, T>
{
public:
    Hash(int slotN);

    bool insert(string key, T val);
    bool remove(string key);
    bool contains(string key);
    bool query(string key, T& val) ;

private:

    // Calculate the String's Hash Key.
    int str2key( string key);
};
模板
类哈希:公共哈希
{
公众:
散列(int-slotN);
布尔插入(字符串键,T val);
bool-remove(字符串键);
bool包含(字符串键);
布尔查询(字符串键、T&val);
私人:
//计算字符串的哈希键。
int str2键(字符串键);
};
但我似乎无法访问类哈希中的字段。为什么?

当你说“我无法访问类哈希中的字段”时,我猜你的意思是,当你使用
哈希
(对于某些类型
T
)时,你不能从
哈希
调用重载函数。原因是名称隐藏:当您重载派生类中的成员函数时,基类中具有相同名称的所有成员都将隐藏,除非您显式地使它们可用。方法是使用声明执行:

template <class T>
class Hash<string, T> : public Hash<int, T>
{
public:
    Hash(int slotN);

    using Hash<int, T>::insert;
    using Hash<int, T>::remove;
    using Hash<int, T>::contains;
    using Hash<int, T>::query;

    bool insert(string key, T val);
    bool remove(string key);
    bool contains(string key);
    bool query(string key, T& val) ;

private:

    // Calculate the String's Hash Key.
    int str2key( string key);
};

(如果它没有任何成员,我也不想定义它)。

您的
散列的主声明没有显示任何模板参数,所以您专门化了什么,我对您显示的代码有点困惑。你能展示你得到的具体代码和错误信息吗。
template <typename T>
bool Hash<string, T>::insert(string key, T val) {
    return this->Hash<int, T>::insert(this->str2key(key, val);
}
template <typename T>
bool Hash<string, T>::insert(string key, T val) {
    int n0 = slotN; // doesn't work: looked up in phase 1
    int n1 = this->slotN; // OK: name is dependent
    int n2 = Hash<int, T>::slotN; // OK, too
}
template <typename K, typename T>
class Hash;