• Advertisement
Sign in to follow this  

Unresolved symbols...WTF?

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
You cant split a template class into multiple files. This is because the code for templates are generated once per cpp file and then copies are removed when linking. Because the templates code is in a cpp file and its not used in that file, the code isn't generated.

Share this post


Link to post
Share on other sites
Ha!

Thanks... I knew it would be something freakin' dumb.

It's all of the little subtleties that are driving me insane.

I appreciate the help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement