mercredi 29 avril 2020

C++ program to simulate FIFO with class templates, returns value 3221225477 when dequeuing

I'm currently studying my second course of C++ object oriented programming at university, so I'll probably have bad programming practices and general errors in my code, so, please, point them out if you see any. I'm always open to learn.

I currently have an assignment on C++ templates, where I have to create a program with a class that simulates FIFO(First in First out) queue using a different class as the template type (Queue< Human >HumanQueue).

Queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include "human.h"


template<class T>
class Queue : public Human{
    public:
        Queue(int = 5);
        ~Queue();
        void enqueue(T);
        T dequeue();
        void PrintQueue();

    private:
        T* array;
        int size, index;
};

#endif

Queue.cpp

#include <iostream>
#include "queue.h"
using namespace std;


template<class T>
Queue<T>::Queue(int s){
    array = new T[s];
    size = s;
    index = 0;
}


template<class T>
Queue<T>::~Queue(){
    delete [] array;
}


// Add object to end of array
template<class T>
void Queue<T>::enqueue(T obj){
    if(index == size){
        cout << "Rinda ir pilna, nevar pievienot elementu!" << endl; // Array full, can't add any more objects
        return;}

    else{
        array[index] = obj;
        index++;}
}


// Remove object from start of array and shift the whole array by 1 position
template<class T>
T Queue<T>::dequeue(){
    for(int i = 0; i < size; i++){
        array[i] = array[i + 1];
    }

    index--;
}


template<class T>
void Queue<T>::PrintQueue(){
    for(int i = 0; i < index; i++){
        cout << i + 1 << ". ";
        array[i].PrintHuman();
    }
}

main.cpp

#include <iostream>
#include "human.h"
#include "queue.h"
#include "queue.cpp"
using namespace std;


int main(){
    Queue<Human> HumanQueue(3);
    Human a("Janis", 1.86, 76);
    Human b("Peteris", 1.76, 69);
    Human c("Arturs", 1.79, 75);
    Human d("Aleksis", 1.81, 78);


    cout << "Elementu rinda" << endl; // Element queue
    HumanQueue.enqueue(a);
    HumanQueue.enqueue(b);
    HumanQueue.PrintQueue();
    cout << "\n//Pievienojam elementu rindai//" << endl; // Add element to queue
    HumanQueue.enqueue(c);
    HumanQueue.PrintQueue();
    cout << "\n//Meginam pievienot vel 1 elementu rindai//" << endl; // Trying to add one more element to queue, should return, that queue is full
    HumanQueue.enqueue(d);
    HumanQueue.PrintQueue();
    cout << "\n//Iznemam 2 elementus no rindas//" << endl; // Dequeue 2 elements from queue
    HumanQueue.dequeue();
    HumanQueue.dequeue();
    HumanQueue.PrintQueue();


    system("pause");
    return 0;
}

The class "Human" is open to interpretation with any variables and functions of my choosing so I'm not including it in this thread.

The constructor, enqueue and print work fine, but when trying to dequeue I get a return value of 3221225477. From what I have gathered, it means it's some kind of problem with the way the program is using the memory. I used this same template for a previous project, where the types were int, char, float and it worked fine, but it doesn't like to work with objects.

Aucun commentaire:

Enregistrer un commentaire