Question

Hi i am tryng to develop a circular queue, but this implementation works fine for basics type like int float and so on,

template<typename R, size_t aSize=200>
class CircularQueue{
public:

    explicit CircularQueue(const R & aElement);


    ~CircularQueue()
    {

    }

    bool push(const R & aElement);

    bool pop(R & aElement);

    bool isEmpty() const    ;

    bool isFull() const     ;
private:
    CircularQueue(){}
    CircularQueue(const CircularQueue &);
    const CircularQueue & operator=(const CircularQueue &);
    int incrementar(int  ) const;
        static const size_t capacity=aSize+1;
        R array[capacity];
        volatile int tail;
        volatile int head;

};

but when i try to speciallized this to a custom type the compiler tells that i have to call the especif constructor: Especif class

    class PutMessage: public  IMetodo, Sujeto<PutMessage>
    {
    public:
       explicit PutMessage(Servant * aServant = 0,Mensaje * aMensaje=0, Observer<PutMessage> * aFuture=0);
        virtual ~PutMessage();
    bool guard() const;
    int getResult() const ;
    void call();

    Mensaje * getMensaje() const;

    Servant * getServant() const;

    bool hasFuture() const;
    private:
    PutMessage();
    Servant * mServant;
    Mensaje * mMensaje;
    int mResult;
    bool mHasFuture;
};
}

the call to the circular queue:

CircularQueue<PutMessage,32> aCircular(*aMessageQueue);

Do i have to reimplement the class to a semispecialization class??

Was it helpful?

Solution

The problem is caused by this data member:

R array[capacity];

An array already contains all its elements, and so a call to R's constructor is required for each one. Why not use std::vector instead?

OTHER TIPS

Stop using the explicit keyword in the way shown in your code. In your PutMessage class, make a void constructor and it should work fine:

PutMessage() : PutMessage(0,0,0) {}

For the record, assign invalid pointers to nullptr instead of 0.

Edit: Although, for a better implementation of CircularQueue, you should really use operator new() to assign your queue space and placement new to construct the elements. However, there might be some design requirement that specifies using the stack instead, which is why this is not part of my original answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top