Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我使用模板实现了队列,以用于自定义数据类型,但出现了错误_C++_Algorithm_Oop_Data Structures - Fatal编程技术网

C++ 我使用模板实现了队列,以用于自定义数据类型,但出现了错误

C++ 我使用模板实现了队列,以用于自定义数据类型,但出现了错误,c++,algorithm,oop,data-structures,C++,Algorithm,Oop,Data Structures,我实现了队列并创建了一个自定义数据类型坐标,我想将x和y成对存储,但我不想使用STL提供的数据类型坐标来进行学习 但是在queue.front实现中,我得到了错误 代码: 错误: 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,

我实现了队列并创建了一个自定义数据类型坐标,我想将x和y成对存储,但我不想使用STL提供的数据类型坐标来进行学习

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

代码:

错误:

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

提前感谢您的帮助

这个非常复杂的错误告诉您,无法将int值为0的NULL转换为坐标

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

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

这个非常复杂的错误告诉您,无法将int值为0的NULL转换为坐标

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

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

两个较小的问题会阻止您的代码编译:

您不应该使用NULL。一点也不。前面返回一个T,即一个对象,但坐标没有constructor,可以让您从NULL构造坐标。使代码与下一点一起编译的修复方法是返回默认构造元素:

T front()
{
    if(empty())
    {
        return {};
    }
// ...
然而,你应该重新考虑在没有前线的情况下你想做什么。当您声明返回T的方法时,您不能返回no T。std::optional可能是一个选项

接下来,您的坐标没有默认构造函数,但在我上面的修复中,您尝试在某些地方默认构造一个构造函数。如果将构造函数更改为

coordinate(int a = 0,int b = 0)
{
    this->x = a;
    this->y = b;
}
然后,可以在不带参数的情况下调用默认构造函数,并且代码编译时不会出错:

但是,您至少应该更改一件事:构造函数的主体不是初始化成员的地方!在构造函数主体运行之前初始化成员。将其更改为:

node(T x) : value(x), next(nullptr)
{
}
相似的

coordinate(int a = 0,int b = 0) : x(a), y(b) 
{
}

两个较小的问题会阻止您的代码编译:

您不应该使用NULL。一点也不。前面返回一个T,即一个对象,但坐标没有constructor,可以让您从NULL构造坐标。使代码与下一点一起编译的修复方法是返回默认构造元素:

T front()
{
    if(empty())
    {
        return {};
    }
// ...
然而,你应该重新考虑在没有前线的情况下你想做什么。当您声明返回T的方法时,您不能返回no T。std::optional可能是一个选项

接下来,您的坐标没有默认构造函数,但在我上面的修复中,您尝试在某些地方默认构造一个构造函数。如果将构造函数更改为

coordinate(int a = 0,int b = 0)
{
    this->x = a;
    this->y = b;
}
然后,可以在不带参数的情况下调用默认构造函数,并且代码编译时不会出错:

但是,您至少应该更改一件事:构造函数的主体不是初始化成员的地方!在构造函数主体运行之前初始化成员。将其更改为:

node(T x) : value(x), next(nullptr)
{
}
相似的

coordinate(int a = 0,int b = 0) : x(a), y(b) 
{
}

你可能想抛出一个异常。C++17和C++03的奇怪混合。我的猜测是C++17 CTAD是偶然的。处理这个问题的另一种方法是创建一个坐标,该坐标具有可测试的无效状态,可以添加一个bool isValid常量;然后把它还给我。然后,客户机代码必须在每次使用之前从代码中检索对象时测试该对象是否有效。还有std::optional,可以类似地使用。从头开始的队列有点有趣。通常最好使用队列包装,比如动态数组或链表。话虽如此,标准库使用deque双端队列作为std::queue和std::stack的基础。如果您只是为了学习而实现这一点,我建议您使用std::vector高效地执行此操作,并记住前面的位置,但不要删除它们。这将使你更熟悉好的C++编程,而不是从头开始实现自己的队列。你可能想抛出一个异常。C++和C++ 17的奇数混合。我的猜测是C++17 CTAD是偶然的。处理这个问题的另一种方法是创建一个坐标,该坐标具有可测试的无效状态,可以添加一个bool isValid常量;然后把它还给我。然后,客户机代码必须在每次使用之前从代码中检索对象时测试该对象是否有效。还有std::optional,可以类似地使用。从头开始的队列有点有趣。通常最好使用队列包装,比如动态数组或链表。话虽如此,标准库使用deque双端队列作为std::queue和std::stack的基础。如果您只是为了学习而实现这一点,我建议您使用std::vector高效地执行此操作,并记住前面的位置,但不要删除它们。这会让你更熟悉好的C++ 编程而不是从头开始实现自己的队列。