如何解决此问题(使用typedef的c ++模板)

时间:2017-02-27 04:22:11

标签: c++ templates

//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!

2 个答案:

答案 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