c++ - C++编译的无限时间

标签 c++ templates

我试图实现一个持久的双端队列结构。但我对 C++ 中的模板不是很熟悉。现在只有一个 push_front 函数,它无法正确编译。

#include <iostream>

using namespace std;

template < typename T > class Container{
public:
    T val;
    Container(){}
    Container(T _val){
        val = _val;
    }
};

template < typename T > class Deque{
public:
    Container<T>* head;
    Container<T>* tail;
    Deque< pair<T, T> > *child;
    Deque<T>(){}
    Deque<T>(Container<T>* _head, Deque< pair<T, T> > *_child, Container<T>* _tail){
        head = _head;
        child = _child;
        tail = _tail;
    }
    Deque<T>* push_front(T x){
        if (this == NULL)
            return new Deque<T>(new Container<T>(x), NULL, NULL);
        if (head == NULL)
            return new Deque<T>(new Container<T>(x), child, tail);
        return new Deque<T>(NULL, child->push_front(make_pair(x, head->val)), tail);
    }
};

int main(){
    Deque<int> d;
    int a = 1;
    d.push_front(a);
}

有push_front函数的算法方案。 http://i.stack.imgur.com/an1xd.jpg

最佳答案

我相信编译器可能会对 Deque<int> 的事实感到困惑有一个 child类型 Deque<pair<int,int>>*它有一个 child类型 Deque<pair<pair<int, int>, pair<int, int> > >*它有 .... 的子项,并尝试实例化所有这些类型。

Deque<int>
    child: Deque<pair<int, int> >*
        child: Deque<pair<pair<int, int> , pair<int, int> > >*
            child: Deque<pair<pair<pair<int, int>....

关于c++ - C++编译的无限时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919653/

相关文章:

c++ - 这个在设计模式中的名称是什么?

c++ - 如何从模板g++获取变量名

c++ - 通过继承类使用来自不同 namespace 的运算符?

c++ - 如何使基模板类函数在派生类中可见?

c++ - 是否可以根据 lambda 采用的参数数量专门化采用 lambda 表达式的模板函数?

templates - 创建适用于ElasticSearch中所有索引模板的正确方法

c++ - 将数据移入函数然后返回到它的来源时是否存在任何未定义的行为问题?

C++ Armadillo princomp() 段错误

c++ - 为什么 reverse_iterator 双重定义其嵌套类型?

C++ Visual Studio 2010 不链接 native 静态库