C++ 在链表末尾插入字符串
我正在链表的末尾插入一个字符串。当我编译我的文件时,我得到2个错误: 错误:“setData”未在此作用域中声明 setData(*字符串p) 错误:“getNext”未在此作用域中声明 newNode=getNext() 但是,它们是在我使用它们之前定义的(在上述方法中定义),因此我不理解错误C++ 在链表末尾插入字符串,c++,C++,我正在链表的末尾插入一个字符串。当我编译我的文件时,我得到2个错误: 错误:“setData”未在此作用域中声明 setData(*字符串p) 错误:“getNext”未在此作用域中声明 newNode=getNext() 但是,它们是在我使用它们之前定义的(在上述方法中定义),因此我不理解错误 #include <iostream> #include <string> using std::string; using std::cout
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
#define SUCCESS 0
#define FAIL 1
// Represents an entry object in the linked-list
class ListEntry
{
public:
explicit ListEntry();
explicit ListEntry(const char *string_p);
~ListEntry();
string getData();
void setData(const char* string_p);
void setData(string string);
ListEntry *getNext();
ListEntry *getPrevious();
ListEntry *prev_p; // pointer to previous entry in the linked-list
ListEntry *next_p; // pointer to next entry in the linked-list
private:
string data; // entry's string
};
// Represents the linked-list object
class List
{
public:
List();
~List();
bool printForward();
bool printReverse();
bool insert(const char *string_p);
private:
int entryCount; // number of entries present in the linked-list
ListEntry *head_p; // pointer to the first entry in the list
ListEntry *tail_p; // pointer to the last entry in the list
};
// ListEntry constructor
ListEntry::ListEntry()
{
this->prev_p = NULL;
this->next_p = NULL;
return;
}
// ListEntry constructor
ListEntry::ListEntry(const char *string_p)
{
this->data = string_p;
this->prev_p = NULL;
this->next_p = NULL;
return;
}
// List entry destructor
ListEntry::~ListEntry()
{
return;
}
// Return the stored string object
string ListEntry::getData()
{
return this->data;
}
// Set the internal string data from a char*
void ListEntry::setData(const char* string_p)
{
this->data = string_p;
}
// Set the internal string data from a string
void ListEntry::setData(string string)
{
this->data = string;
}
// Returns reference to the next entry in the list
ListEntry *ListEntry::getNext()
{
return this->next_p;
}
// Returns reference to the previous entry in the list
ListEntry *ListEntry::getPrevious()
{
return this->prev_p;
}
#包括
#包括
使用std::string;
使用std::cout;
使用std::endl;
#定义成功0
#定义失败1
//表示链接列表中的条目对象
课堂旁听
{
公众:
显式ListEntry();
显式ListEntry(const char*string_p);
~ListEntry();
字符串getData();
void setData(常量字符*字符串p);
void setData(字符串);
ListEntry*getNext();
ListEntry*getPrevious();
ListEntry*prev\u p;//指向链接列表中上一个条目的指针
ListEntry*next\u p;//指向链接列表中下一个条目的指针
私人:
字符串数据;//条目的字符串
};
//表示链接列表对象
班级名单
{
公众:
List();
~List();
bool printForward();
bool printReverse();
布尔插入(常量字符*字符串);
私人:
int entryCount;//链表中存在的条目数
ListEntry*head\u p;//指向列表中第一个条目的指针
ListEntry*tail\u p;//指向列表中最后一个条目的指针
};
//ListEntry构造函数
ListEntry::ListEntry()
{
此->上一页p=NULL;
此->下一步p=NULL;
返回;
}
//ListEntry构造函数
ListEntry::ListEntry(常量字符*字符串)
{
此->数据=字符串\u p;
此->上一页p=NULL;
此->下一步p=NULL;
返回;
}
//列表项析构函数
ListEntry::~ListEntry()
{
返回;
}
//返回存储的字符串对象
字符串ListEntry::getData()
{
返回此->数据;
}
//从字符中设置内部字符串数据*
void ListEntry::setData(常量字符*字符串)
{
此->数据=字符串\u p;
}
//从字符串中设置内部字符串数据
void ListEntry::setData(字符串)
{
此->数据=字符串;
}
//返回对列表中下一项的引用
ListEntry*ListEntry::getNext()
{
返回此->下一步;
}
//返回对列表中上一项的引用
ListEntry*ListEntry::getPrevious()
{
返回此->上一页;
}
和我的插入函数(在我的程序中的上述方法下面):
bool列表::插入(常量字符*字符串)
{
//请编写列表插入函数
//要插入的新节点
ListEntry*newNode=newlistentry();
//List*newList=newList();
if(newNode==NULL)
{
cout head_p=NULL)
{
newNode=getNext();//此处出错
newNode=this->head\u p;
this->head\u p=newNode;//newNode现在指向head节点
这->entryCount++;
回归成功;
}
其他的
{
ListEntry*temp=this->head\u p;
while(temp->next_p!=NULL)
{
温度=温度->下一步;
}
temp->next_p=newNode;
这->entryCount++;
回归成功;
}
}
}
您已经定义了函数,但没有按照您定义的方式使用它们:
setData(*string_p); // Takes a const char*, but you have provided a char.
// *string_p dereferences the string pointer, giving the
// first char.
newNode = getNext(); // getNext is a ListEntry function, but you are trying
// to use it in the context of List. This is also true of the
// above function.
函数
setData
和getNext
是类ListEntry
的非静态成员函数。因此,必须使用成员访问表达式调用它们
此外,此调用提供的参数
setData(*string_p);
与函数期望的类型不同
你至少要像我一样写作
newNode->setFata( string_p );
及
尽管从语法的角度来看,即使函数的调用是正确的,这个代码片段也没有意义
if(this->head_p = NULL)
{
newNode = newNode->getNext();
newNode = this->head_p;
因为至少存在内存泄漏
还有这个if语句
if(newNode == NULL)
如果您使用新接线员的以下呼叫,将有意义
ListEntry* newNode = new ( std::nothrow ) ListEntry();
该函数可以按以下方式显示
bool List::insert( const char *string_p )
{
//new node to be inserted
ListEntry *newNode = new ( std::nothrow ) ListEntry( string_p );
bool success = newNode != nullptr;
if ( success )
{
if ( tail_p )
{
tail_p->next_p = newNode;
newNode->prev_p = tail_p;
}
else
{
head_p = newNode;
}
tail_p = newNode;
entryCount++;
}
return success;
}
您的insert()
方法实现完全错误。它应该看起来更像这样:
int List::insert(const char *string_p)
{
//new node to be inserted
ListEntry* newNode = new ListEntry(string_p);
if (newNode == NULL)
{
cout << "FAILED";
return FAIL;
}
if (this->head_p == NULL) {
this->head_p = newNode;
}
if (this->tail_p != NULL)
{
this->tail_p->next_p = newNode;
newNode->prev_p = this->tail_p;
}
this->tail_p = newNode;
this->entryCount++;
return SUCCESS;
}
int List::insert(常量字符*字符串)
{
//要插入的新节点
ListEntry*newNode=newlistentry(字符串p);
if(newNode==NULL)
{
cout head_p==NULL){
此->头\u p=newNode;
}
如果(此->尾部p!=NULL)
{
this->tail\u p->next\u p=newNode;
newNode->prev\u p=this->tail\u p;
}
此->tail\u p=newNode;
这->entryCount++;
回归成功;
}
另外,setData(*string_p);应该是setData(string_p);setData
和getNext
属于ListEntry
,但是您将它们与this
一起在属于List
的方法中使用。您的意思是newNode->getNext();
和newNode->setData(string_p);
?但是setData()取'const char*string\p',我传递'const char*string\p'。有相同的类型吗?@guy不,你传递的是*string\p
,它是const char*
取消引用的。也就是说,一个char。所以如果我不取消引用,即去掉*,它们应该是同一类型的吗?@guy:正确。不要添加星号,它会编译。idea如果head指针为null,则将newNode设为新的head指针。我的代码现在可以编译,但我有seg错误。有什么想法吗?@guy在编写代码时没有意义。有什么建议吗?@guy阅读您自己的代码。首先,您动态分配了一个对象,其地址存储在变量bewNode中。然后,v变量被覆盖。因此分配内存的地址丢失。@请参阅我更新的帖子。
bool List::insert( const char *string_p )
{
//new node to be inserted
ListEntry *newNode = new ( std::nothrow ) ListEntry( string_p );
bool success = newNode != nullptr;
if ( success )
{
if ( tail_p )
{
tail_p->next_p = newNode;
newNode->prev_p = tail_p;
}
else
{
head_p = newNode;
}
tail_p = newNode;
entryCount++;
}
return success;
}
int List::insert(const char *string_p)
{
//new node to be inserted
ListEntry* newNode = new ListEntry(string_p);
if (newNode == NULL)
{
cout << "FAILED";
return FAIL;
}
if (this->head_p == NULL) {
this->head_p = newNode;
}
if (this->tail_p != NULL)
{
this->tail_p->next_p = newNode;
newNode->prev_p = this->tail_p;
}
this->tail_p = newNode;
this->entryCount++;
return SUCCESS;
}