2010-07-30 54 views
0

我目前正在创建一个循环双向链表作为练习。这个练习模仿了该死的东西,这被证明是相当痛苦的。经过许多错误清除后,我得到了更多的错误。我会对此大笑,但现在我已经很疲惫,精疲力竭了。布尔运算符缺少模板参数?

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; 
} 

模板与诠释为输入工作,但不能与字符串,这是我目前正试图去上班。

最后一个功能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'

有什么不对吗?

+3

你应该给一些更多的信息。比如,你在哪里传递std :: vector ?到底是什么? – Zoltan 2010-07-30 23:08:52

+0

将main()调用添加到函数以及输入和工作输入(int工作) – IAE 2010-07-30 23:15:21

+2

这里缺少一些东西。 'CDLinkedList'似乎没有'display'成员函数,并且调用'operator()'的地方在哪里? – 2010-07-30 23:19:34

回答

4

您缺少std::string的比较运算符。尝试在您的源文件中添加一个

#include <string> 

其中包含main

包括<iostream>给你一个std::string的前向声明。这是因为<iostream>允许您执行大量的字符串操作(例如,它允许您通过使用字符串流将字符串从/转换为几乎任何东西)。然而,这个前向声明并没有给你任何比较操作符。您需要包含<string>

对于它的价值,你可能会尽量减少你的测试用例来

#include <iostream> 

bool f() { 
    std::string a, b; 
    return a != b; 
} 

来证明这个问题。这个测试用例由于完全相同的原因而无法编译,其中包括<string>使其工作。

3

我要在黑暗中进行野生刺探。

我认为你应该#include <string>而不是<vector>。您可能会看到字符串的前向声明,但没有<string>我认为您没有正确的operator==operator!=过载可见。