C++ can';t调用基模板类';派生类中的构造函数
我有这样的层次结构:C++ can';t调用基模板类';派生类中的构造函数,c++,templates,inheritance,C++,Templates,Inheritance,我有这样的层次结构: #include <boost/shared_ptr.hpp> //base cache template <typename KEY, typename VAL> class Cache; //derived from base to implement LRU template <typename KEY, typename VAL> class LRU_Cache :public Cache<KEY,VAL>{};
#include <boost/shared_ptr.hpp>
//base cache
template <typename KEY, typename VAL> class Cache;
//derived from base to implement LRU
template <typename KEY, typename VAL> class LRU_Cache :public Cache<KEY,VAL>{};
//specialize the above class in order to accept shared_ptr only
template <typename K, typename VAL>
class LRU_Cache<K, boost::shared_ptr<VAL> >{
public:
LRU_Cache(size_t capacity)
{
assert(capacity != 0);
}
//....
};
//so far so good
//extend the LRU
template<typename K, typename V>
class Extended_LRU_Cache : public LRU_Cache<K,V >
{
public:
Extended_LRU_Cache(size_t capacity)
:LRU_Cache(capacity)// <-- this is where the error comes from
{
assert(capacity != 0);
}
};
#包括
//基本缓存
模板类缓存;
//从基础派生以实现LRU
模板类LRU_缓存:公共缓存{};
//专门化上述类,以便仅接受共享的ptr
模板
类LRU_缓存{
公众:
LRU缓存(大小和容量)
{
断言(容量!=0);
}
//....
};
//到目前为止还不错
//扩展LRU
模板
类扩展的LRU缓存:公共LRU缓存
{
公众:
扩展缓存(大小容量)
:LRU_Cache(capacity)/调用基本构造函数时,需要父级的完整定义(即使用模板参数):
Extended\u LRU\u缓存(大小\u t容量)
:LRU_缓存(容量)
{
断言(容量!=0);
}
您需要指定基本类型的模板参数:
Extended_LRU_Cache(size_t capacity)
:LRU_Cache<K, V>(capacity)
{
assert(capacity != 0);
}
尝试使用私有基typedef:typedef LRU缓存base;
并将其用作构造函数。您也可以直接使用LRU缓存
,但这可能会让人感到困惑。啊,输入错误……谢谢大家
Extended_LRU_Cache(size_t capacity)
:LRU_Cache<K,V>(capacity)
{
assert(capacity != 0);
}
Extended_LRU_Cache(size_t capacity)
:LRU_Cache<K, V>(capacity)
{
assert(capacity != 0);
}
template <typename T>
class Base
{
public:
Base() { }
};
template <typename T, typename U>
class Derived : Base<T>, Base<U>
{
public:
// error: class ‘Derived<T, U>’ does not have any field named ‘Base’
// Derived() : Base() { }
// ^
// Which Base constructor are we calling here?
// But this works:
Derived() : Base<T>(), Base<U>() { }
};