gpt4 book ai didi

c++ - 如何在不出现此错误的情况下将 listnode 值设置为 Null?

转载 作者:行者123 更新时间:2023-12-02 09:49:27 28 4
gpt4 key购买 nike

我只是想花一点时间感谢 Remy Lebeau 为我编辑了这篇文章。

我的编译器给了我这个错误:

main.cpp:93:20: error: invalid conversion from ‘int’ to ‘listnode*’ [-fpermissive]
temp -> next = NULL;



这是在 InsertTail方法。

我不明白问题出在哪里。有人可以帮我解决这个问题吗?我只需要能够将 listnode 值设置为 Null。

这是我的 C++ 文件:
program4.cpp
#include <iostream>

using namespace std;

#undef NULL

const int NULL = 0;

typedef int element;

const element SENTINEL = -1;

element read_element();

class listnode{

public:
element data;
listnode * next;

};

class LList {

private:
listnode * head;
listnode * tail;

public:
LList();
~LList();
void Print();
void InsertHead(element thing);
void InsertTail(element thing);
element DeleteHead();
void ReadForward();
void ReadBackward();
void Clean();
void Steal(LList & Victim);
void Append(LList & Donor);
void Duplicate(LList & Source);
void Reverse();

};

void LList::Print(){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, and its elements
// have been displayed

listnode * temp;

temp = head;
while (temp != NULL){
cout << temp -> data << endl;
temp = temp -> next;
}

}

void LList::ReadForward(){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of new listnodes containing elements
// given by the user in forward order

element userval;

Clean();
cout << "Enter elements, " << SENTINEL << " to stop: ";
userval = read_element();
while (userval != SENTINEL){
InsertTail(userval);
userval = read_element();
}

}

void LList::InsertTail(element thing){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the tail-end of the list

listnode * temp;

temp = new listnode;
temp -> data = thing;
temp -> next = NULL;
if (head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;

}

element read_element(){

// PRE: the user must enter a series of zero or
// more non-valid element values, followed
// by a valid element value
//
// POST: all entered non-valid element values will
// be successfully discarded, and the first
// valid element value entered will be
// returned

element userval;

cin >> boolalpha >> userval;
while (! cin.good()){
cin.clear();
cin.ignore(80, '\n');
cout << "Invalid data type, should be an element, "
<< "try again: ";
cin >> boolalpha >> userval;
}
return userval;
}

void LList::ReadBackward(){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of new listnodes containing elements
// given by the user in backward order

element userval;

Clean();
cout << "Enter elements, " << SENTINEL << " to stop: ";
userval = read_element();
while (userval != SENTINEL){
InsertHead(userval);
userval = read_element();
}

}


void LList::InsertHead(element thing){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the head-end of the list

listnode * temp;

temp = new listnode;
temp -> data = thing;
temp -> next = head;
if (head == NULL)
tail = temp;
else
;
head = temp;

}


void LList::Clean(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid and empty, and all of
// its listnodes have been deleted

while (head != NULL)
DeleteHead();

}

element LList::DeleteHead(){

// PRE: the N. O. LList is valid and not empty
// POST: the N. O. LList is unchanged, except that the
// listnode at the head end of the list has been
// deleted, and its data element has been
// returned

listnode * temp;
element thing;

temp = head;
head = head -> next;
thing = temp -> data;
delete temp;
return thing;
}

LList::LList(){

// PRE: none
// POST: the N. O. LList is valid and empty

head = NULL;
}

LList::~LList(){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid and empty, and its
// listnodes have been deleted

Clean();
}

void LList::Steal(LList & Victim){

// PRE: the N. O. and Victim LLists are valid
// POST: the Victim LList is valid and empty
// the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of the listnodes originally on the
// Victim LList

Clean();
head = Victim.head;
tail = Victim.tail;
Victim.head = NULL;

}

void LList::Append(LList & Donor){

// PRE: the N. O. and Donor LLists are valid
// POST: the Donor LList is valid and empty
// the N. O. LList is valid, and it now consists
// of its own original listnodes followed by the
// listnodes originally on the Donor LList

if (head != NULL)
tail -> next = Donor.head;
else
head = Donor.head;
if (Donor.head != NULL)
tail = Donor.tail;
else
;
Donor.head = NULL;
}

void LList::Duplicate(LList & Source){

// PRE: the N. O. and Source LLists are valid
// POST: the Source LList is unchanged
// the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of listnodes containing the same
// elements and in the same order as on the
// Source LList

listnode * temp;

Clean();
temp = Source.head;
while (temp != NULL){
InsertTail(temp -> data);
temp = temp -> next;
}

}

void LList::Reverse(){

// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except its
// elements are in reverse order

listnode * temp;
LList Helper;

temp = head;
while (temp != NULL){
Helper.InsertHead(temp -> data);
temp = temp -> next;
}
Steal(Helper);

}

iny main(){

cout << "creating/constructing LList object L" << endl;
LList L;
cout << "L has been created/constructed" << endl;

cout << "L is calling its print method" << endl;
L.Print();
cout << "L has been printed" << endl;

cout << "L is calling its ReadForward method" << endl;
L.ReadForward();
cout << "L has been read forward" << endl;

cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;

cout << "L is calling its ReadBackward method" << endl;
L.ReadBackward();
cout << "L has been read backward" << endl;

cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;

cout << "L is calling its Clean method" << endl;
L.Clean();
cout << "L has been cleaned" << endl;

cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;

}

最佳答案

此代码错误的原因

#undef NULL

const int NULL = 0;

目前尚不清楚您为什么决定使用它。但是为了避免错误,只需删除这两行。

本声明
const int NULL = 0;

不引入空指针常量。

此外,而不是 NULL你可以使用 nullptr .

注意类 listnode应该是类 LList 的私有(private)成员.

例如
class LList {

private:

struct listnode{
element data;
listnode * next;
} *head = nullptr, *tail = nullptr;
//...

关于c++ - 如何在不出现此错误的情况下将 listnode 值设置为 Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61466765/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com