I've recently went back to using a lot of C++ after using Java for quite a while, and I expected a little annoyance as I remembered how things worked (under the hood) in C++, but this is becoming very frustrating...
Is there any reason why VS.NET 2003 would be giving me unresolved external symbol link errors when there are no external linked libraries and all headers are included and the functions are declared in the headers (as part of classes)?
I was trying to brush up on some templates and stuff, so I went way back and read the memory manager article from Enginuity Part II, then went to implement it myself for the practice.
Here the class that's giving me trouble... as you can see, I barely even started coding it... It's probably something stupid, but if anyone can see the cause, I'd appreciate being filled in.
//Here is the class definition... this is all there is to it so far.
template <class T>
class MMObjPtr
{
protected:
T *obj;
public:
MMObjPtr();
MMObjPtr(T *obj);
MMObjPtr(MMObjPtr<T> &other);
~MMObjPtr();
};
//here is the content of the .cpp file
#include "MMObjPtr.h"
template <class T>
MMObjPtr<T>::MMObjPtr()
{
obj = NULL;
}
template <class T>
MMObjPtr<T>::MMObjPtr(T *obj)
{
this->obj = obj;
if(obj != NULL)
obj->addRef();
}
template <class T>
MMObjPtr<T>::MMObjPtr(MMObjPtr<T> &other)
{
obj = other.obj;
if(obj != NULL)
obj->addRef();
}
template <class T>
MMObjPtr<T>::~MMObjPtr()
{
if(obj != NULL)
obj->release();
}
Here is the gist of main() that uses that class... and produces the unresolved symbol stuff
TestObject *ptrs[5];
for(int i = 0; i < 5; i++)
{
ptrs = new TestObject(i);
ptrs->addRef();
}
MMObjPtr<TestObject> testPtr(ptrs[1]);
printf("Number of live objects: %d\n", MMObject::getTotalLiveObjects());
printf("Number of dead objects: %d\n", MMObject::getTotalDeadObjects());
printf("Memory used by objects : %u\n\n", MMObject::getMemUsage());
printf("Removing all references...\n\n");
for(int i = 0; i < 5; i++)
ptrs->release();
printf("Number of live objects: %d\n", MMObject::getTotalLiveObjects());
printf("Number of dead objects: %d\n", MMObject::getTotalDeadObjects());
printf("Memory used by objects : %u\n\n", MMObject::getMemUsage());
testPtr = (TestObject*)NULL;
printf("Invoking garbage collector...\n\n");
MMObject::garbageCollect();
printf("Number of live objects: %d\n", MMObject::getTotalLiveObjects());
printf("Number of dead objects: %d\n", MMObject::getTotalDeadObjects());
printf("Memory used by objects: %u\n\n", MMObject::getMemUsage());
MMObject::collectRemainingGarbage();
Oh yeah, and the link errors...
Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall MMObjPtr<class TestObject>::~MMObjPtr<class TestObject>(void)" (??1?$MMObjPtr@VTestObject@@@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall MMObjPtr<class TestObject>::MMObjPtr<class TestObject>(class TestObject *)" (??0?$MMObjPtr@VTestObject@@@@QAE@PAVTestObject@@@Z) referenced in function _main
EDIT: I should add that the compile output above makes it appear as though the other source isn't included in the project, but it is. That build I pasted above didn't have any changes in the others.