我目前正在创建一个循环双向链表作为练习。该练习正在模拟该死的东西,事实证明这很痛苦。经过许多、许多、许多错误删除后,我得到了更多错误。我会笑的,但我现在很累很累。
Node.h
template<class T>
class Node
{
public:
Node(T val) : data(val), next(0), prev(0) {}
Node(T val, Node *next, Node *prev) : data(val), next(next), prev(prev) {}
Node() : data(0), next(0), prev(0) {}
~Node()
{}
Node *next;
Node *prev;
T data;
};
.
LinkedList.h // Superclass
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include "Node.h"
enum Direction
{
Forward,
Backward
};
template<class T>
class LinkedList
{
public:
virtual void push_back(T data) = 0;
virtual void push_front(T data) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual void insert_before(T data, int index) = 0;
virtual void insert_after(T data, int index) = 0;
virtual void pop_before() = 0;
virtual void pop_after() = 0;
virtual void display(Direction direction = Forward) = 0;
virtual int length() const = 0;
virtual T operator[](int index) = 0;
virtual Node<T> *operator()(T data) = 0;
};
#endif
.
CDLinkedList.h // Circular Doubly-Linked List
template<class T>
class CDLinkedList : public LinkedList<T>
{
public:
/* Functions go here */
Node<T> *operator()(T data)
{
Node<T> *temp = head;
for( int i(0);
i < length()-1 && temp->data != data;
++i, temp = temp->next )
continue;
if( temp->data == data )
return temp;
else
{
std::cerr << "Error: Element not found." << std::endl;
return 0;
}
}
void display(Direction direction = Forward)
{
std::ostream_iterator<T> oIter(std::cout, " ");
if( direction == Forward )
{
Node<T> *temp = head;
for( int i(0); i < length(); ++i, temp = temp->next )
oIter = temp->data;
}
else
{
Node<T> *temp = tail;
for( int i(0); i < length(); ++i, temp = temp->prev )
oIter = temp->data;
}
}
.
#include <iostream>
#include <vector>
int main( int argc, char** argv )
{
using std::cout;
using std::endl;
using std::cin;
using std::string;
CDLinkedList<std::string> list;
list.push_back("Hello");
list.push_back(",");
list.push_back("World.");
cout << "Displaying normally..." << endl;
list.display();
cout << "Displaying backwards..." << endl;
list.display(::Direction::Backward);
cin.get();
return 0;
}
模板使用 int 作为输入,但不适用于字符串,这是我目前正在尝试的工作。
最后一个功能Node *operator()(T data)
是我当前的问题孩子。我得到的错误是:
error C2784: 'bool std::operator !=(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::string'
这里有什么问题?