[英]Calling member function of template grandparent
我正在尝试制作多键映射的模板(只是习惯了可变参数模板)。 这是代码:
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&key1,const KeyOthers&... keys)中收到``没有匹配函数''错误。 编译器看不到祖父母函数,也不搜索该类。 如何告诉编译器执行此操作?
我怀疑这与本问题中的阴影有关 。 但是我不知道如何编写“ using”指令,该指令引用层次结构中最基本的类。 我应该使用类似的是 ?
编译跟踪为:
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...);
您的insert
方法隐藏了基类的insert
方法。
您必须using QMultiKeyMap<Value, Key2, Keys...>::insert;
在派生类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.