Home > OS >  How can I enqueue a structure object into a queue when both are templates?
How can I enqueue a structure object into a queue when both are templates?

Time:07-31

I am trying to push a structure object into a linked list or array queue. I'm using this data structure to meet the following condition:

"Customer history: this function adds customers to your data structure. If the data structure is full, it will delete the very old customer"

The struct holds three variables, the customer's name, email and phone number, and they will all be of the string data type. A requirement of the project is that all functions, classes, and data structures have to be a template, and I am having trouble enqueuing the struct into the queue. I'll supply the code I have below:

This is my struct:


template <class T>
struct Customer {
    T Cname; //Customer Name
    T Cemail;  //Customer E-mail
    T Cphnum; //Customer Phone

    Customer<T> (T n, T e, T phn) {
       Cname=n; 
       Cemail=e;
       Cphnum=phn;
    }

    T get_name() {
        return Cname;
        cout << Cname;
    }

    T get_email() {
        return Cemail;
    }

    T get_phnum() {
        return Cphnum;
    }
} ;

This is the function to input the information into the struct and then into the queue:

template <class T>
void input_customer_info(T n, T e, T phn) {

    //Customer Information
    T Cn = n;   //Customer Name
    T Ce = e;  //Customer E-mail
    T Cph = phn;  //Customer Phone Number

    Customer<string> c(Cn ,Ce ,Cph);
    

    Queue<string> CI;
    CI.enQueue(c);
}

And this is my main function:

int main(){
    int input = 0;
    while (input != 1) {
        //User Input of Information 
        string nm, em, ph;
        cout << "Please Enter Your Name: " << endl;
        cin >> nm;
        cout << "Please Enter Your Email Address: " << endl;
        cin >> em;
        cout << "Please Enter Your Phone Number: " << endl;
        cin >> ph;
        
        //Function to input the Customer's info into a queue
        input_customer_info(nm, em, ph);

        cout << "Enter 1 or 0: \n1 to stop entering  Customer Info, \n0 to enter more Customer Info" << endl;
        cin >> input;
    }

}

This is the Linked List Queue implementation I am first testing with, but I might go with the array implementation because it allows me to set a limit to the queue:

template <typename T>
struct QNode {
    T data;
    QNode* next;
    int count;
    QNode(T d)
    {
        count = 0;
        data = d;
        next = NULL;
    }
};

template <typename T>
struct Queue {
    QNode<T>* front, * rear;
    Queue()
    {
        front = rear = NULL;
    }

    void enQueue(T x)
    {

        // Create a new Linked-List node
        QNode<T>* temp = new QNode<T>(x);

        // If queue is empty, then
        // new node is front and rear both
        if (rear == NULL) {
            front = rear = temp;
            return;
        }

        // Add the new node at
        // the end of queue and change rear
        rear->next = temp;
        rear = temp;
    }

    // Function to remove
    // a key from given queue q
    void deQueue()
    {
        // If queue is empty, return NULL.
        if (front == NULL)
            return;

        // Store previous front and
        // move front one node ahead
        QNode<T>* temp = front;
        front = front->next;

        // If front becomes NULL, then
        // change rear also as NULL
        if (front == NULL)
            rear = NULL;

        delete (temp);
    }
};

Array Queue Implementation:

template <class T>
struct AQueue {
    int front, rear;
    int capacity;
    int* queue;

    
    AQueue (int c) {
        front = rear = 0;
        capacity = c;
        queue = new int;
    }

    ~AQueue() { delete[] queue; }

    void enqueue(T data) {
        if (capacity == rear) {
            cout << "Queue is full\n";
            return;
        }
        else {
            queue[rear] = data;
            rear  ;
        }
        return;
    }

    void dequeue() {
        if (front == rear) {
            cout << "Queue is empty\n";
            return;
        }
        else {
            for (int i = 0; i < rear - 1; i  ) {
                queue[i] = queue[i   1];
            }
            rear--;
        }
        return;
    }
};

Where I'm having trouble is at the "input_customer_info" function where I'm trying to enqueue the struct object "c" into the linked list queue "CI". I'm getting the following error in the image below:

Image of error C2664 description

I don't really know how to fix it, so if I can get some help that would be great. Thanks.

CodePudding user response:

The first thing I would ask is why have you templated the Customer class? Is there a specific reason, i.e do you imagine T will be an stream or int etc. ? You have created a Customer<string> and a Queue<string> yet are pushing the Customer and hence the error not being able to convert between Customer<string> and string. Your queue should be a queue of customers.

  • Related