0

Trying to do a simple enqueue/dequeue operation by generating binary numbers but ran into an error in line#18 Main:

    #include <iostream>
#include "quetype.cpp"
#include "quetype.h"
#include <queue>

using namespace std;

int main()
{

    QueType<string> q;
    int n = 10;

    q.Enqueue("1");

    while (n--)
    {
        string s1 = q.front(); //expression cant be used as a function
        q.Dequeue();
        cout << s1 << "\n";
        string s2 = s1;  
        q.Enqueue(s1.append("0"));
        q.Dequeue(s2.append("1"));

    }

}

this happened after i changed the type of the variable front from private to public (It was giving an error saying front is private before i made it public)

The .h file:

#ifndef QUETYPE_H_INCLUDED
#define QUETYPE_H_INCLUDED

class FullQueue
{};
class EmptyQueue
{};
template<class ItemType>
class QueType
{
    public:
        QueType();
        QueType(int max);
        ~QueType();
        void MakeEmpty();
        bool IsEmpty();
        bool IsFull();
        void Enqueue(ItemType);
        void Dequeue(ItemType&);
        int front;
        //int rear;
        //ItemType* items;
        //int maxQueue;
    private:
        //int front;
        int rear;
        ItemType* items;
        int maxQueue;
};

#endif // QUETYPE_H_INCLUDED

The source cpp:

#include"quetype.h"
#include <queue>
template<class ItemType>
QueType<ItemType>::QueType(int max){
    maxQueue=max+1;
    rear=max;
    front=max;
    items= new ItemType[ maxQueue];
}
template<class ItemType>
QueType<ItemType>::QueType(){
    maxQueue=501;
    rear=maxQueue-1;
    front=maxQueue-1;
    items= new ItemType[ maxQueue];
}
template<class ItemType>
QueType<ItemType>::~QueType(){
    delete[] items;
}
template<class ItemType>
void QueType<ItemType>::MakeEmpty(){
    rear=maxQueue-1;
    front=maxQueue-1;
}
template<class ItemType>
bool QueType<ItemType>::IsEmpty(){
    return (rear==front);
}
template<class ItemType>
bool QueType<ItemType>::IsFull(){
    return ((rear+1)%maxQueue==front);
}
template<class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem){
    if(IsFull())
        throw FullQueue();
    else{
        rear=(rear+1)%maxQueue;
        items[rear]=newItem;
    }
}
template<class ItemType>
void QueType<ItemType>::Dequeue(ItemType& Item){
    if(IsEmpty())
        throw EmptyQueue();
    else{
        front=(front+1)%maxQueue;
        Item=items[front];
    }
}

what did i do wrong?

1 Answers1

0

In your Quetype class front is member variable not member function, you have to remove ( ).

Just write

string s1= q.front;
Lux
  • 17,835
  • 5
  • 43
  • 73
Arsh
  • 1
  • 1