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

テンプレートは入力として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'

ここで何が問題になっていますか?

4

2 に答える 2

4

の比較演算子が不足していstd::stringます。追加してみてください

#include <string>

を保持するソースファイル内main

を含める<iostream>と、の前方宣言が得られますstd::string。これ<iostream>は、多くの文字列操作を実行できるためです(たとえば、文字列ストリームを使用して、文字列をほぼすべてのものとの間で変換できます)。ただし、この前方宣言では、比較演算子は提供されません。そのために含める必要があります<string>

それが価値があるもののために、あなたはあなたのテストケースを最小化することができたでしょう

#include <iostream>

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

この問題を実証するため。このテストケースは、まったく同じ理由でコンパイルに失敗し、含める<string>と機能します。

于 2010-07-30T23:55:27.253 に答える
3

私は暗闇の中で野生の刺し傷を取るつもりです。

私はあなたがすべきであり、すべき#include <string>ではないと思います<vector>。文字列の前方宣言が表示されている可能性がありますが、これがないと、正しいオーバーロードが表示<string>されているとは思いません。operator==operator!=

于 2010-07-30T23:48:00.460 に答える