首页 > 解决方案 > 我使用模板实现了队列以与我的自定义数据类型一起使用,但出现错误

问题描述

我实现了队列并创建了一个自定义数据类型“坐标”(我想将 x 和 y 存储为一对,但我不想使用 STL 提供的对(用于学习目的))

但是在 queue.front() 实现中我得到了错误

代码:

#include<iostream>
using namespace std;

template <class T>

class node{
    public:
    T value;
    node <T> *next;
    node(T x)
    {
        value = x;
        next = NULL;
    }
};

template <class T>
class queue{
    node<T> *start;
    node<T> *end;

    public:

    queue()
    {
        start = NULL;
        end = NULL;
    }

    bool empty()
    {
        return start == NULL;
    }

    void push(T v)
    {
        node<T> *temp = new node<T>(v);
        if(empty())
        {
            start = temp;
            end = temp;
        }
        else
        {
            end->next = temp;
            end = temp;
        }
        return;
    }

    void pop()
    {
        if(empty())
        {
            return;
        }
        else
        {
            node<T> *temp = start;
            start = start->next;
            delete temp;
        }
        return;
    }

    T front()
    {
        if(empty())
        {
            return NULL;
        }
        else
        {
            return start->value;
        }
    }

};

class coordinate
{
public:
    int x;
    int y;
    coordinate(int a,int b)
    {
        this->x = a;
        this->y = b;
    }
};

int main()
{
    coordinate p =coordinate(1,2);
    cout<<p.x;                              // getting expected output of 1
    pair a = make_pair(1,2);
    queue<coordinate> q;
    q.push(coordinate(1,2));
    cout<<q.front().x;
    return 0;   
}

错误:

In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/stddef.h:1,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/include/stdint.h:32,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/stdint.h:9,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/cstdint:41,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:501,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
                 from E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:1:
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp: In instantiation of 'T queue<T>::front() [with T = coordinate]':
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:102:16:   required from here
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:73:11: error: could not convert '0' from 'long long int' to 'coordinate'
    return NULL;
           ^~~~
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp: In instantiation of 'node<T>::node(T) [with T = coordinate]':
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:40:19:   required from 'void queue<T>::push(T) [with T = coordinate]'
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:101:24:   required from here
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:14:2: error: no matching function for call to 'coordinate::coordinate()'
  {
  ^
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:88:2: note: candidate: 'coordinate::coordinate(int, int)'
  coordinate(int a,int b)
  ^~~~~~~~~~
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:88:2: note:   candidate expects 2 arguments, 0 provided
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:83:7: note: candidate: 'constexpr coordinate::coordinate(const coordinate&)'
 class coordinate
       ^~~~~~~~~~
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:83:7: note:   candidate expects 1 argument, 0 provided
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:83:7: note: candidate: 'constexpr coordinate::coordinate(coordinate&&)'
E:\Coding\CP\solid-octo-engine\DataStructures and Algorithms\Queue\QueueImplementationUsingLL.cpp:83:7: note:   candidate expects 1 argument, 0 provided

提前感谢您的帮助

标签: c++algorithmoopdata-structures

解决方案


这个非常复杂的错误告诉您无法将NULL(即 int value 0)转换为coordinatein

T front()
{
    if(empty())
    {
        return NULL; //here
    }
    else
    {
        return start->value;
    }
}

这是真的,没有办法转换。empty()如果返回,您需要抛出异常,true或者让队列的用户接受未定义的行为并且不采取任何措施来防止此类问题。

T front()
{
    return start->value; //UB if empty, but that's users' fault
}

推荐阅读