我正在从事一个项目,该项目需要我模拟可以在行的前面和后面添加和删除的客户列表。为此,我选择了使用双向链接列表数据结构,并创建了两个类Client和ClientLine来完成它。这两个课程的标题都可以在下面看到。
class Client
{
public:
//Default constructor.
//This constructor automatically fills the QueryTime private member
//with a random value upon creation.
Client();
//***** setQueryTime *****
//A function to set the QueryTime private member to a specific value.
void setQueryTime(float SetTime);
//***** getQueryTime *****
//A function to get the private member, QueryTime.
float getQueryTime();
private:
//A variable to hold the time it will take the secretary to answer
//the question.
float QueryTime;
}; //class Client
class ClientLine
{
public:
//A node to go into the list of clients.
typedef struct{
struct clientNode *next; //Points to the next node in the list
struct clientNode *prev; //Points to the previous node in the list
Client currentClient; //The client in the current spot in line.
int clientType; //Tells whether the client is in person or on the phone.
} clientNode;
//Default constructor
//Creates an empty doubly-linked list to represent a
//line of clients (phone and in person).
ClientLine();
//***** isLineEmpty *****
//Boolean function to tell if there is nobody in line.
//The line is empty if listHeader->front == listHeader->rear == NULL.
bool isLineEmpty();
//***** addClientRear *****
//Adds a new client to the rear of the line.
void addClientRear(Client newClient, int clientType);
//***** addClientFront *****
//Adds a new client to the front of the line.
void addClientFront(Client newClient, int clientType);
//***** removeClientRear *****
//Deletes the node at the rear of the list and returns the client from it.
Client removeClientRear();
//***** removeClientFront *****
//Deletes the node at the front of the list and returns the client from it.
Client removeClientFront();
private:
clientNode *front; //Pointer to the front of the list
clientNode *rear; //Pointer to the rear of the list
int listLength; //A variable to hold the length of the list.
}; //class ClientLine
在我的addClientRear成员函数中,我尝试使用以下代码在新节点中分配指针,以指向列表中的上一个节点:
//***** addClientRear *****
//Adds a new client to the rear of the line.
void ClientLine::addClientRear(Client newClient, int clientType){
//Make a new node to contain the client.
clientNode *newNode = new clientNode;
newNode->currentClient = newClient;
newNode->clientType = clientType;
//Set the pointers in the new node and list as needed.
if(isLineEmpty()){
front = newNode;
rear = newNode;
newNode->next = NULL;
newNode->prev = NULL;
}
else{
newNode->prev = rear;
newNode->next = NULL;
}
listLength += 1; //increase the length of the list by one.
} //addClientRear()
但是我在声明newNode-> prev = Rear的语句时出错:
不能将类型为“ ClientLine :: clientNode *”的值分配给类型为“ clientNode *”的实体
我不确定为什么会出现此错误,因为我试图将一个指针值分配给另一个指针。任何建议将不胜感激。
答案 0 :(得分:0)
您有两个不同的clientNode
结构声明(如错误消息所示)。用C ++方式而不是C方式定义结构。
struct clientNode {
clientNode *next; //Points to the next node in the list
clientNode *prev; //Points to the previous node in the list
Client currentClient; //The client in the current spot in line.
int clientType; //Tells whether the client is in person or on the phone.
};
如果不清楚,这是您的版本,带有一些注释
typedef struct{
struct clientNode *next; // this forward declares clientNode
struct clientNode *prev;
Client currentClient;
int clientType;
} clientNode; // this defines ClientLine::clientNode
两个声明是不同的。