我实现了queue并创建了一个自定义数据类型& &;coordinates&;(我想将x和y作为一对存储,但我不想使用STL提供的pair(用于学习目的))
但是在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:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:1:
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp: In instantiation of 'T queue<T>::front() [with T = coordinate]':
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:102:16: required from here
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:73:11: error: could not convert '0' from 'long long int' to 'coordinate'
return NULL;
^~~~
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp: In instantiation of 'node<T>::node(T) [with T = coordinate]':
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:40:19: required from 'void queue<T>::push(T) [with T = coordinate]'
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:101:24: required from here
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:14:2: error: no matching function for call to 'coordinate::coordinate()'
{
^
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:88:2: note: candidate: 'coordinate::coordinate(int, int)'
coordinate(int a,int b)
^~~~~~~~~~
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:88:2: note: candidate expects 2 arguments, 0 provided
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:83:7: note: candidate: 'constexpr coordinate::coordinate(const coordinate&)'
class coordinate
^~~~~~~~~~
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:83:7: note: candidate expects 1 argument, 0 provided
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:83:7: note: candidate: 'constexpr coordinate::coordinate(coordinate&&)'
E:CodingCPsolid-octo-engineDataStructures and AlgorithmsQueueQueueImplementationUsingLL.cpp:83:7: note: candidate expects 1 argument, 0 provided
提前感谢您的帮助
这个非常复杂的错误告诉您没有办法将
中的NULL
(它是int值0
)转换为coordinate
。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
}
有两个小问题会阻碍你的代码编译:
return NULL;
你不应该使用NULL
。一点也不你的front
返回一个T
,即一个对象,但coordinates
没有构造函数,让你从NULL
构造一个coordinate
。使您的代码编译(与下一点一起)的一个修复是返回一个默认的构造元素:
T front()
{
if(empty())
{
return {};
}
// ...
但是,当没有front
时,您应该重新考虑要做什么。当您声明该方法返回T
时,您不能返回T
。也许std::optional
是一个选项。
接下来,您的coordinate
没有默认构造函数,但您尝试在某些地方默认构造一个(也在我上面的修复中)。如果将构造函数更改为
coordinate(int a = 0,int b = 0)
{
this->x = a;
this->y = b;
}
那么这是一个默认构造函数(可以不带参数调用),代码编译时不会出错:https://godbolt.org/z/8E97ca.
然而,你至少应该改变一件事:构造函数体不是初始化成员的地方!在构造函数体运行之前初始化成员。改为:
node(T x) : value(x), next(nullptr)
{
}
类似和
coordinate(int a = 0,int b = 0) : x(a), y(b)
{
}