I'm trying a controlled, learning experiment with my project and it involves creating my own collections and iterators, essentially an array and a linked list. There is something that I'm missing since it compiles with linking errors. I've spent three days examining, coding and recoding this and I really need some help.
I'm using Visual Studio 2010. I'm dabbling in the new C++11 stuff with the new ranged based for, or for each as I think of it. In Visual C++ it is for each (VAR in LIST)
, but in GCC it is for (VAR : LIST)
.
Here is the linker errors:
main.obj : error LNK2001: unresolved external symbol "public: virtual class Iterator<int> __thiscall Container<int>::begin(void)const " (?begin@?$Container@H@@UBE?AV?$Iterator@H@@XZ)
main.obj : error LNK2001: unresolved external symbol "public: virtual class Iterator<int> __thiscall Container<int>::end(void)const " (?end@?$Container@H@@UBE?AV?$Iterator@H@@XZ)
My code is as follows:
template<typename T>
class Iterator
{
public:
Iterator(T* Start) : Current(Start) { }
const T& operator*() const { return *Current; }
const T* operator->() const { return Current; }
Iterator<T>& operator++() { Current++; return *this; }
bool operator!=(Iterator<T> &Other) { return Current != Other.Current; }
protected:
T* Current;
};
template<typename T>
class Container
{
public:
Container() : Count(0) { }
Container(unsigned int Count) : Count(Count) { }
unsigned int GetCount() const { return Count; }
bool IsEmpty() const { return Count == 0; }
Iterator<T>& GetIterator() const { return begin() };
// Compatibility with C++0x range-based for requires begin() and end() functions.
virtual Iterator<T> begin() const;
virtual Iterator<T> end() const;
protected:
unsigned int Count;
};
template<typename T>
class ArrayList : public Container<T>
{
public:
ArrayList() : Items(nullptr), Container(0) { }
virtual Iterator<T> begin() const
{
return Iterator<T>(Items);
}
virtual Iterator<T> end() const
{
return Iterator<T>(Items + Count);
}
private:
T *Items;
};
int main()
{
ArrayList<int> MyList;
for each (auto Item in MyList)
{ }
return MyList.GetCount();
}