我在链表的末尾插入一个字符串。当我编译我的文件时,我得到2个错误:
错误:未在此范围内声明'setData' 使用setData(* string_p);
错误:未在此范围内声明'getNext' newNode = getNext();
然而,在我使用它们之前定义它们(在上面的方法中定义),所以我不理解错误。
#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;
}
我的插入功能(在我的程序中低于上述方法):
bool List::insert(const char *string_p)
{
// Please write the list insert function
//new node to be inserted
ListEntry* newNode = new ListEntry();
//List *newList = new List();
if(newNode == NULL)
{
cout << "FAILED";
}
else
{
setData(*string_p); //////ERROR HERE
if(this->head_p = NULL)
{
newNode = getNext(); //////ERROR HERE
newNode = this->head_p;
this->head_p = newNode; // newNode now points to the head node
this->entryCount++;
return SUCCESS;
}
else
{
ListEntry* temp = this->head_p;
while(temp -> next_p != NULL)
{
temp = temp -> next_p;
}
temp -> next_p = newNode;
this->entryCount++;
return SUCCESS;
}
}
}
答案 0 :(得分:1)
您已定义了这些功能,但未按照您定义的方式使用它们:
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.
答案 1 :(得分:0)
函数setData
和getNext
是类ListEntry
的非静态成员函数。因此必须使用成员访问表达式调用它们。
此外,此调用提供的参数
setData(*string_p);
的类型与函数期望的类型不同。
你必须写至少像
newNode->setFata( string_p );
和
newNode->getNext();
虽然这个代码片段即使从语法的角度来看函数的调用是正确的也没有意义
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;
}
答案 2 :(得分:0)
您的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;
}