【发布时间】:2017-09-01 05:25:30
【问题描述】:
我的主函数调用链表中的删除函数
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
if(myList.remove(argument))
{
cout << "Successfully removed the element from the list\n";
}
else
{
cout << "Could not remove the element from the list\n";
}
break;
我的结构和函数调用看起来像这样
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node{
int val;
node* next;
node* prev;
};
// Linked list class definition
class LL{
public:
LL();
void prepend(int);
void append(int);
void remove(int);
bool removeFront();
bool removeBack();
node* search(int);
void print();
void deleteList();
private:
node* head;
};
#endif
mycpp 文件看起来像这样
#include "ll.h"
LL::LL()
{
head = NULL;
}
void LL::remove(int num){
该函数搜索以num为值的节点,如果找到则从列表中删除
node* second = head;
node* first = head->next;
if (head == NULL)
{
return;
}
else if (head->val == num)
{
node* temp = head;
head = head->next;
delete temp;
}
while (first&&first->val != num)
{
second = first;
first = first->next;
}
if (first)
{
second->next = first->next;
delete first;
}
}
错误是无法将 myList remove 参数从 void 转换为 BOOL 但我在说什么认为 BOOL 函数?我不会传回 true 或 false 值。
【问题讨论】:
-
确切的错误信息是什么,它指向哪一行?
-
在函数'int main()'中:main.cpp:66:29:错误:无法将'myList.LL::remove(argument)'从'void'转换为'bool'如果(mylist.remove(argument))
-
说真的,您有一个使用 C++ 编译器的 C++ 程序,并且您认为选择的最佳语言标签是 C - 并且只有 C?
-
它被标记在 C++ 和链表中
标签: c++ linked-list