如何定义一个继承自模板类的模板类?
我想将std::queue
和std::priority_queue
包装到一个基类中,我的情况是LooperQueue
。
我这样使用StdQueue
:auto queue = new StdQueue<LooperMessage *>()
。
我的类定义让编译器抱怨。
错误日志:
In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:14:5: error: unknown type name 'size_type'; did you mean 'size_t'?
size_type size() override;
^~~~~~~~~
size_t
/Users/rqg/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/5.0.300080/include/stddef.h:62:23: note: 'size_t' declared here
typedef __SIZE_TYPE__ size_t;
^
In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:16:5: error: unknown type name 'reference'
reference front() override;
^
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:20:21: error: unknown type name 'value_type'; did you mean 'ARect::value_type'?
void push(const value_type &x) override;
^~~~~~~~~~
ARect::value_type
/Users/rqg/Library/Android/sdk/ndk-bundle/sysroot/usr/include/android/rect.h:44:21: note: 'ARect::value_type' declared here
typedef int32_t value_type;
代码:
#ifndef PBOTEST_LOOPERQUEUE_H
#define PBOTEST_LOOPERQUEUE_H
#include <queue>
#include <cstdlib>
template<typename Tp, typename Sequence = std::deque<Tp> >
class LooperQueue {
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
typedef typename Sequence::size_type size_type;
typedef Sequence container_type;
virtual size_type size() = 0;
virtual reference front() = 0;
virtual void pop()= 0;
virtual void push(const value_type &x) = 0;
};
#endif //PBOTEST_LOOPERQUEUE_H
#ifndef PBOTEST_STDQUEUE_H
#define PBOTEST_STDQUEUE_H
#include "LooperQueue.h"
template<typename Tp, typename Sequence = std::deque<Tp> >
class StdQueue : public LooperQueue<Tp, Sequence> {
public:
size_type size() override;
reference front() override;
void pop() override;
void push(const value_type &x) override;
private:
std::queue<Tp, Sequence> mQueue;
};
#endif //PBOTEST_STDQUEUE_H
using
而不是typedef
”的含义。是否有更多细节可以提供? - Fantasy_RQG