Reputation: 25
I have a class derived from vector, and its data elements hold structs. My problem is that when I attempt to use swap or any other method of moving the structs from one element to another I get errors. Here is my class and the struct:
#ifndef PRIQUEUE
#define PRIQUEUE
#include "PriQueueError.h"
#include <vector>
using std::vector;
const int EMPTY_QUEUE = 1;
template <class T>
struct qElem
{
T Data;
int Priority;
qElem(T data, int priority) :Data(data), Priority(priority){};
};
template <class T, class T2>
class PriQueue : public vector<T2>
{
public:
PriQueue(){}
PriQueue(T data, int priority);
void enqueue(T data, int priority);
T dequeue() throw(PriQueueError);
T qPeek() throw(PriQueueError);
int qSize();
};
template <class T, class T2>
PriQueue<T, T2>::PriQueue(T data, int priority)
{
this->enqueue(data, priority);
}
template <class T, class T2>
void PriQueue<T, T2>::enqueue(T data, int priority)
{
qElem<T> tmp1(data, priority);
this->push_back(tmp1);
bool flag(false);
if (this->qSize() > 1)
{
for (int i = this->qSize() - 1; i > 0 || flag == false; i--)
{
flag = true;
if (tmp1.Priority < tmp2.Priority)
{
std::swap(this[i], this[i-1]);
flag = false;
}
}
}
}
template <class T, class T2>
T PriQueue<T, T2>::dequeue() throw(PriQueueError)
{
if (this->empty())
throw PriQueueError("Empty Queue!");
T tmp = this->qPeek();
this->pop_back();
return tmp;
}
template <class T, class T2>
T PriQueue<T, T2>::qPeek() throw(PriQueueError)
{
if (this->empty())
throw PriQueueError("Empty Queue!");
qElem<T> tmpStruct = this->back();
T tmpData = tmpStruct.Data;
return tmpData;
}
template <class T, class T2>
int PriQueue<T, T2>::qSize()
{
return this->size();
}
#endif
The enqueue function is a bit messy because of the constant changes I have made to it to make it work. Everywhere I read it says that to access the data member of the vector you would just use nameofclass[index].member or this[index].member. In my main when I create an instance of the class (qInt) I can access the struct using qInt[index].Data, but I cannot via this[index].Data. When I am inside enqueue and try to swap(this[index], this[anotherIndex]) it errors. When I try pull up the information on this[0] and this[1] inside the debugger watch window which should give me the first and second index location of the vector, it tells me that everything is inside this[0]. I am lost on what im doing here, my class is derived from vector and therefore should act like a vector I thought, but it seems to not be doing that at all.
Upvotes: 0
Views: 95
Reputation: 141554
Deriving from vector (or any other standard container) is a bad idea. They're designed to be class members ; you should make a member variable of type vector<T2>
instead. The derivation doesn't gain anything, but it does introduce problems (slicing, deleting through base class pointer, awkward syntax in your member functions, etc).
Your immediate issue is that this
is a pointer, so instead of this[0]
you need (*this)[0]
, or this->operator[](0)
.
Upvotes: 3