我最近一直在努力复习c++,这样一些语法错误就会出现在我面前,但当我试图运行下面的代码时,我似乎得到了错误: C2440。它在main的第一行中断,并指向行:122
特别是这一行: Node* target_node = find_node_by_data(info);
初始化时出现错误C2440 :无法从Doublely_Linked_list::Node转换为void *‘
感谢您的光临并帮助我解决这个问题。
template<typename AnyType>
void Doublely_Linked_list<AnyType>::find_node_by_data(AnyType const& information)const {
Node* current = head;
while (current) {
if (current->information == information) {
return current;
}
current = current->next;
}
return nullptr;
}
template<typename AnyType>
void Doublely_Linked_list<AnyType>::unlinkData(Doublely_Linked_list<AnyType>::Node* n) {
if (n->prev) {
n->prev->next = n->next;
}
else {
head = n->next;
head->prev = nullptr;
}
if (n->next) {
n->next->prev = n->prev;
}
else {
tail = n->prev;
tail->next = nullptr;
}
}
template<typename AnyType>
void Doublely_Linked_list<AnyType>::remove(AnyType info) {
Node* target_node = find_node_by_data(info);
if (target_node) {
unlinkData(target_node);
delete target_node;
}
}
int main() {
Doublely_Linked_list<double> list1;
double temp;
const double info = 2.1;
while (1)
{
cin >> temp;
if (temp == -1) break;
else
{
list1.insertAtHead(temp);
}
}
cout << list1;
list1.remove(info);
cout << list1;
}在我用值填充节点的控制台中,我想填充节点值'2.1‘,并在填充完DLL后将其删除。
发布于 2019-07-21 15:23:37
很明显,find_node_by_data被声明为返回void
template<typename AnyType>
void Doublely_Linked_list<AnyType>::find_node_by_data(AnyType const& information)const {
^^^^当它显然应该返回一个节点指针时
template<typename AnyType>
Node* Doublely_Linked_list<AnyType>::find_node_by_data(AnyType const& information)const {
^^^^^发布于 2019-07-22 06:12:50
约翰,谢谢你的帮助,离开电脑一段时间后,我终于把它修好了。我对这个函数的解决方案是:
template<typename AnyType>
typename Doublely_Linked_list<AnyType>::Node*
Doublely_Linked_list<AnyType>::find_node_by_data(AnyType const& information)const {
Node* current = head;
while (current) {
if (current->data == information) {
return current;
}
current = current->next;
}
return nullptr;
}https://stackoverflow.com/questions/57131154
复制相似问题