Hello,
I am having trouble with the error message in the title: "Left operand must be l-value." I created a template class as follows:
Code:
template <class T>
class vector
{
T *ptr;
int size;
vector(const int&);
public:
vector();
vector(const vector&);
vector operator =(const vector&);
void operator >>(const T&);
int mag();
T operator()(const int&) const;
//void operator <<(const T&); //add this later, just to be fancy
~vector();
friend ostream& operator <<(ostream&, vector&);
};
template <class T>
vector<T>::vector()
{
size = 0;
ptr = NULL;
}
template <class T>
vector<T>::vector(const vector &another)
{
size = another.size;
ptr = new T [size];
for (int i=0; i<size; i++)
{
ptr[i] = another.ptr[i];
}
}
template <class T>
vector<T>::vector(const int &a)
{
size = a;
ptr = new T [a];
}
template <class T>
void vector<T>::operator >>(const T &addition)
{
vector<T> temp(size+1);
for (int i=0; i<size; i++)
{
temp.ptr[i] = ptr[i];
}
temp.ptr[size] = addition;
delete [] ptr;
size = temp.size;
ptr = new T [size];
for (i=0; i<size; i++)
{
ptr[i] = temp.ptr[i];
}
return;
}
template <class T>
T vector<T>::operator()(const int &a) const
{
return ptr[a];
}
template <class T>
int vector<T>::mag()
{
return size;
}
template <class T>
vector<T> vector<T>::operator =(const vector &another)
{
vector temp(another.size);
temp.ptr = new T [size];
for (int i=0; i<temp.size; i++)
{
temp.ptr[i] = another.ptr[i];
}
return temp;
}
template <class T>
vector<T>::~vector()
{
delete [] ptr;
}
After doing this, I created a regular struct:
Code:
struct BGT
{
bool DC;
};
You don't have to go through all the code if you don't want to. I just included it for convenience. The problem arises when I use the () operator to set the DC value. I can tell you that the vector overloaded operator works fine. But when I do something like this:
Code:
vector<BGT> bgt;
/*
...
after increasing the size of the vector
...
*/
bgt(index).DC = true; //THIS is where I get the error
I just can't understand why. Can anyone please help?
Many, MANY thanks in advance.
Bill