Original Post
Hi All, I am implementing (for educational reasons) a Templated dynamic sized list in C++. I've come across a problem with operator overloading, and I can't see what I am doing wrong. I have my template class defined like this: And I am using it like this: I get a compiler error: Error 1 error C2440: '=' : cannot convert from 'mylist' to 'int' On the "val = theList;" line. I can't see what I am doing wrong here? Any ideas? Thanks!
template<typename Type> class mylist
{
public:
mylist<Type>()
{
// allocate the initial storage (default to 10)
initialize(10);
}
mylist<Type>(int capacity)
{
// allocate the initial storage
initialize(capacity);
}
~mylist<Type>()
{
m_count = -1;
m_capacity = -1;
delete[] m_data;
m_data = NULL;
}
void Add(const Type &item);
Type operator[] (int index);
Type getVal(int index)
{
return m_data[index];
}
private:
Type* m_data;
int m_count;
int m_capacity;
void initialize(int capacity);
void resize();
};
// irrelevant implementations snipped
template<typename Type> Type mylist<Type>::operator[] (int index)
{
// leave out bounds checking for now
return m_data[index];
}
mylist<int>* theList = new mylist<int>();
int count = 50000;
for (int i = 0; i < count; ++i)
{
theList->Add(i * 3);
}
int val = -1;
for (int i = 0; i < count; ++i)
{
val = theList;
cout << val << endl;
}
delete theList;