调用模板祖父母的成员函数

时间:2014-06-05 15:42:41

标签: c++ qt templates

我正在尝试制作多键映射模板(只是为了习惯可变参数模板)。这是代码:

template<class Value, class Key1, class ...Keys>
class QMultiKeyMap;

// single key map
template<class Value, class Key1>
class QMultiKeyMap<Value, Key1>
{
    QMap<Key1, Value*> data;

protected:
    // ignore inserting without keys
    void insert(Value*) {} // This is the function I want to call

public:
    QMultiKeyMap() {}

    void insert(const Value& v, const Key1& k1)
    {
        Value* pV = new Value(v);
        data.insert(k1, pV);
    }
};

// map with at least two keys
template<class Value, class Key1, class Key2, class ...Keys>
class QMultiKeyMap<Value, Key1, Key2, Keys...>: public QMultiKeyMap<Value, Key2, Keys...>
{
    QMap<Key1, Value*> data;

protected:
    typedef QMultiKeyMap<Value, Key2, Keys...> base;

    // inserting with mix of keys, but with Key1 too
    // (cases without Key1 are inherited)
    template<class ...KeyOthers>
    void insert(Value* pV, const Key1& k1, const KeyOthers& ...keys)
    {
        // in case keys... is not empty, call itself recursively.
        // If keys... is empty, then there's the empty function insert(Value*) in grandparent class (QMultiKeyMap with one key) which should be called.
        insert(pV, keys...);
        data.insert(k1, pV);
    }

public:
    QMultiKeyMap() {}

    // inserting value with mix of keys
    template<class KeyOne, class ...KeyOthers>
    void insert(const Value& v, const KeyOne& k1, const KeyOthers& ...keys)
    {
        Value* pV = new Value(v);
        insert(pV, k1, keys...);
    }
};

我正打算打电话:

QMultiKeyMap<int, quint32, quint16, quint8> test;

// single key insertion - the value is 1, the key is quint32(1)
test.insert(1, quint32(1));

但是我在QMultiKeyMap :: insert(Value * pV,const Key1&amp; key1,const KeyOthers&amp; ...键)中得到'无匹配函数'错误。 编译器没有看到祖父项功能,也没有搜索该类。我该如何告诉编译器呢?

我怀疑它与this question中的阴影有关。但我不知道应该如何编写'using'指令,该指令引用层次结构的最基类。我应该使用像that这样的东西吗?

编译跟踪是:

In file included from ..\signalTest\main.cpp:1:0:
..\signalTest\handler.h: In instantiation of 'void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(Value*, const Key1&, const KeyOthers& ...) [with KeyOthers = {}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]':
..\signalTest\handler.h:91:38:   required from 'void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(const Value&, const KeyOne&, const KeyOthers& ...) [with KeyOne = unsigned int; KeyOthers = {}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]'
..\signalTest\main.cpp:8:89:   required from here
..\signalTest\handler.h:67:27: error: no matching function for call to 'QMultiKeyMap<int, unsigned int, short unsigned int, unsigned char>::insert(int*&)'
         insert(pV, keys...);
                           ^
..\signalTest\handler.h:67:27: note: candidates are:
..\signalTest\handler.h:64:43: note: template<class ... KeyOthers> void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(Value*, const Key1&, const KeyOthers& ...) [with KeyOthers = {KeyOthers ...}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]
     void insert(Value* pV, const Key1& k1, const KeyOthers& ...keys)
          ^
..\signalTest\handler.h:64:43: note:   template argument deduction/substitution failed:
..\signalTest\handler.h:67:27: note:   candidate expects 3 arguments, 1 provided
         insert(pV, keys...);
                           ^
..\signalTest\handler.h:88:56: note: template<class KeyOne, class ... KeyOthers> void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(const Value&, const KeyOne&, const KeyOthers& ...) [with KeyOne = KeyOne; KeyOthers = {KeyOthers ...}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]
     void insert(const Value& v, const KeyOne& k1, const KeyOthers& ...keys)
          ^
..\signalTest\handler.h:88:56: note:   template argument deduction/substitution failed:
..\signalTest\handler.h:67:27: note:   cannot convert 'pV' (type 'int*') to type 'const int&'
         insert(pV, keys...);

1 个答案:

答案 0 :(得分:1)

您的insert方法隐藏了基类insert方法。

您必须在派生类中添加using QMultiKeyMap<Value, Key2, Keys...>::insert;