我的主函数调用链表中的remove函数
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
if(myList.remove(argument))
{
cout << "Successfully removed the element from the listn";
}
else
{
cout << "Could not remove the element from the listn";
}
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
值。
问题是remove
被声明为void
函数,并且它不返回值。因此,您不能在if()
语句中使用它,因为它不会返回可以转换为布尔值并进行测试的值。
取出呼叫myList.remove(argument)
:周围的if()
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
myList.remove(argument);
break;
由于函数不返回值,因此无法报告它是否成功。如果您真的需要这样做,您必须更改函数的定义,使其返回布尔值。