//trep.h
#include <map>
template <class type>
typedef std::pair<Trep<type>, Trep<type>> TrepPair; //error!
template <class type>
class Trep {
public:
type key;
int priority, size;
Trep *left, right;
Trep(type _key) :
key(_key), priorty(rand()), size(1), left(NULL), right(NULL) {}
TrepPair<type> splited(Trep &root, type key);
Trep* insert(Trep &root, Trep &node);
};
错误是“std :: pair:模板参数太少” 无论我预先清理TrepClass,它都会出现。 我做错了吗?
帮帮我PLZ!
答案 0 :(得分:1)
没有&#34;模板typedef&#34;在C ++中。
相反,C ++ 11引入了template using
。
template <class type>
using TrepPair=std::pair<Trep<type>, Trep<type>>;
您还需要Trep<type>
的前瞻性声明。
答案 1 :(得分:0)
对typedef
使用pair
时,您没有定义Trep
类。因此错误:“'Trep'
未在此范围内声明”
您可以在类规范中移动typedef:
template <class type>
class Trep {
public:
type key;
int priority, size;
Trep *left, right;
Trep(type _key) :
key(_key), priority(rand()), size(1), left(NULL), right(NULL) {}
typedef std::pair<Trep<type>, Trep<type> > TrepPair; //error!
TrepPair splited(Trep &root, type key);
Trep* insert(Trep &root, Trep &node);
};
此外,构造函数成员初始值设定项列表中priority
的拼写错误。它拼写错误为priorty
#include <cstdlib>
rand()
我能够编译代码here