布尔运算符缺少模板参数?

发布于 2024-09-12 18:03:32 字数 3094 浏览 6 评论 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 作为输入,但不使用 string,这正是我当前正在尝试的工作。

最后一个函数 Node *operator()(T data) 是我当前的问题子。我得到的错误是:

error C2784: 'bool std::operator !=(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : 可以不推断 'const std::vector<_Ty,_Alloc> 的模板参数&' from 'std::string'

这里出了什么问题?

I'm currently creating a circular doubly-linked list as exercise. The exercise is templating the damn thing, which is proving to be quite a pain. After many, many, many error-removals I get more errors. I'd laugh at that, but I'm quite tired and exhausted now.

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

The templates work with int as input, but not with string, which is what I'm currently trying to get to work.

The last function Node *operator()(T data) is my current problem child. The error I get is:

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'

What's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-09-19 18:03:32

您缺少 std::string 的比较运算符。尝试

#include <string>

在包含 main 的源文件中添加一个。

包含 将为您提供 std::string 的前向声明。这是因为 允许您执行大量字符串操作(例如,它允许您使用字符串流将字符串转换为几乎任何内容)。但是,此前向声明不会为您提供任何比较运算符。您需要为此添加

对于它的价值,您可以最小化您的测试用例来

#include <iostream>

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

演示这个问题。该测试用例由于完全相同的原因而无法编译,并且包含 使其可以工作。

You are lacking the comparison operators for std::string. Try adding an

#include <string>

in your source file which holds main.

Including <iostream> gives you a forward declaration of std::string. That's because <iostream> allows you to do a lot of string operations (for instance, it allows you to convert a string from/to pretty much anything by using string streams). However this forward declaration does not give you any comparison operators. You need to include <string> for that.

For what it's worth, you could have minimized your test case to

#include <iostream>

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

to demonstrate this issue. This test case fails to compile for exactly the same reason, and including <string> makes it work.

狼性发作 2024-09-19 18:03:32

我要在黑暗中进行一次疯狂的刺杀。

我认为你应该 #include 而不是 。您可能有一个可见的字符串前向声明,但没有 我认为您没有正确的 operator==operator!=< /code> 重载可见。

I'm going to take a wild stab in the dark.

I think that you should #include <string> and not <vector>. You may have a forward declaration of string visible but without <string> I don't think that you have the correct operator== and operator!= overloads visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文