Не удается получить указатель узла, указывающий на другой узел в двусвязном списке - PullRequest
0 голосов
/ 21 января 2019

Я работаю над проектом, который требует, чтобы я имитировал список клиентов, которые я могу добавлять и удалять как с начала, так и с конца линии. Я решил использовать для этого двунаправленную структуру данных списка и создал два класса, 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 = tail, которое говорит:

значение типа "ClientLine :: clientNode *" не может быть присвоено объекту типа "clientNode *"

Я не уверен, почему я получаю эту ошибку, поскольку я пытаюсь присвоить значение указателя другому указателю. Любой совет будет высоко ценится.

1 Ответ

0 голосов
/ 21 января 2019

У вас есть два разных объявления структуры 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

Две декларации разные.

...