抽象模板类和模板childs

时间:2015-03-02 15:04:44

标签: c++ templates generic-programming

我在为DoubleLinkedList创建基类时遇到问题。

现在它给了我这个错误

  

/tmp/cc3lORia.o :( rodata._ZTV24AbstractDoubleLinkedListIiE [_ZTV24AbstractDoubleLinkedListIiE] + 0×10):   未定义的引用   `AbstractDoubleLinkedList :: createNewNode(int)'collect2:错误:   ld返回1退出状态

我已经尝试了这个,你可以通过代码中的注释行看到它,但它们都不起作用。

那么如何用抽象方法(顺便说一下工厂方法)定义抽象模板类,然后在子类中重新定义呢?

/*
 * AbstractDoubleLinkedList.hpp
 *
 *  Created on: Mar 2, 2015
 *      Author: michael
 */

#ifndef ABSTRACTDOUBLELINKEDLIST_H_
#define ABSTRACTDOUBLELINKEDLIST_H_

#include <vector>

using namespace std;


template <class T> class ListNode {

private:
    void init();
public:
    ListNode();
    ListNode(T value);
    ListNode *previous;
    ListNode *next;
    T value;

};

template <class T> void ListNode<T>::init() {
    previous = nullptr;
    next = nullptr;
}

template <class T> ListNode<T>::ListNode() {
    init();
}

template <class T> ListNode<T>::ListNode(T value) {
    init();
    this->value = value;
}

template <class T> class AbstractDoubleLinkedList {

private:

    void pullOutNode(ListNode<T> *node);

protected:

    virtual ListNode<T>* createNewNode(T element);

public:
    AbstractDoubleLinkedList();
    void push_back(T element);
    T front();
    T back();
    void insertBefore(ListNode<T> *node, ListNode<T> *beforeNode);
    void insertAfter(ListNode<T> *node, ListNode<T> *afterNode);
    void moveNodeAfter(ListNode<T> *node, ListNode<T> *afterNode);
    vector<T> toVector();
    ListNode<T> *frontNode;
    ListNode<T> *backNode;
};


template <class T> void AbstractDoubleLinkedList<T>::push_back(T element) {
    ListNode<T>* node = createNewNode(element);
    node->previous = backNode;
    if (backNode != nullptr) {
        backNode->next = node;
        node->previous = backNode;
    }
    else {
        frontNode = node;
    }
    backNode = node;
}

template <class T> void AbstractDoubleLinkedList<T>::pullOutNode(ListNode<T> *node) {
    if (node != frontNode) {
        node->previous->next = node->next;
    }
    else {
        frontNode = node->next;
    }
    if (node != backNode) {
        node->next->previous = node->previous;
    }
    else {
        backNode = node->previous;
    }
}

template <class T> T AbstractDoubleLinkedList<T>::front() {
    return frontNode->value;
}

template <class T> T AbstractDoubleLinkedList<T>::back() {
    return backNode->value;
}

template <class T> void AbstractDoubleLinkedList<T>::insertAfter(ListNode<T> *node, ListNode<T> *afterNode) {
    node->previous = afterNode;
    node->next = afterNode->next;
    afterNode->next = node;
    if (afterNode == backNode) {
        backNode = node;
    }
}

template <class T> void AbstractDoubleLinkedList<T>::insertBefore(ListNode<T> *node, ListNode<T> *beforeNode) {
    node->next = beforeNode;
    beforeNode->previous->next = node;
    beforeNode->previous = node;
    if (beforeNode == frontNode) {
        frontNode = node;
    }
}

template <class T> void AbstractDoubleLinkedList<T>::moveNodeAfter(ListNode<T> *node, ListNode<T> *afterNode) {
    pullOutNode(node);
    node->previous = afterNode;
    node->next = afterNode->next;
    if (node->next == nullptr) {
        backNode = node;
    }
    afterNode->next = node;

}


template <class T> vector<T> AbstractDoubleLinkedList<T>::toVector() {
    ListNode<T>* node = frontNode;
    vector<int> listAsVector;
    bool shouldHaveAnother = (frontNode != nullptr);
    while(shouldHaveAnother) {
        listAsVector.push_back(node->value);
        if (node->next != nullptr)
            node = node->next;
        else {
            shouldHaveAnother = false;
        }
    }
    return listAsVector;
}


template <class T>  AbstractDoubleLinkedList<T>::AbstractDoubleLinkedList() {
    frontNode = nullptr;
    backNode = nullptr;
}


#endif /* ABSTRACTDOUBLELINKEDLIST_HPP_ */



/*
 * DoubleLinkedList.hpp
 *
 *  Created on: Feb 26, 2015
 *      Author: michael
 */

#ifndef DOUBLELINKEDLIST_HPP_
#define DOUBLELINKEDLIST_HPP_

#include "AbstractDoubleLinkedList.hpp"

template <class T> class DoubleLinkedList : public AbstractDoubleLinkedList<T> {

protected:
    ListNode<T>* createNewNode(T element) {
            return new ListNode<T>(element);

        }
public:
    ~DoubleLinkedList() {
        ListNode<T>* node = this->backNode;
        bool shouldHaveAnother = (node != nullptr);
        while(shouldHaveAnother) {
            ListNode<T>* ptr = node->previous;
            delete node;
            if (ptr != nullptr)
                node = ptr;
            else {
                shouldHaveAnother = false;
            }
        }
    };



};

//template <class T> ListNode<T> DoubleLinkedList<T>::createNewNode(T element) {
//  return new ListNode<T>(element);
//}

//template <class T> DoubleLinkedList<T>::~DoubleLinkedList() {
//
//  ListNode<T>* node = this->backNode;
//  bool shouldHaveAnother = (node != nullptr);
//  while(shouldHaveAnother) {
//      ListNode<T>* ptr = node->previous;
//      delete node;
//      if (ptr != nullptr)
//          node = ptr;
//      else {
//          shouldHaveAnother = false;
//      }
//  }
//}


#endif /* DOUBLELINKEDLIST_HPP_ */

EDIT1: 所以,有一个问题“为什么我需要自己的容器类,为什么不使用列表或向量?”

我需要一个具有恒定时间随机访问和恒定时间删除和插入的数据结构。但是我已经尝试过std :: unordered_set了,因为内存的分配和解除分配(删除和插入时)不够好(技术上满足要求)

所以我想到了另一种方式。我想使用带有一个“guard”元素和结尾的链表。当我需要从中删除元素时,我会在守卫后移动它。并检查列表是否为“空”我会检查第一个元素是否为保护元素。但我需要一个恒定的随机访问。这可以通过指向每个元素的指针来实现。

但要实现最高性能,我还需要最大限度地减少缓存未命中。我认为std :: list会分散在内存中,因为它是正常的行为。所以我认为唯一的方法就是分配矢量&gt;然后将此预分配节点用于新元素。

我在某处错了吗?我可以更轻松地实现最佳性能吗?

0 个答案:

没有答案