1

As long as I don't call the function, everything is fine, but once I call the function I get an unresolved external symbol. All of my classes are in the SSE namespace (my own) and have worked fine up until now. Let me show.

#include "SDL.h"
#include "Game.h"
#include "GameObject.h"
#include <tchar.h>

SSE::Game Pong;

int _tmain(int argc, char* argv[])
{
    SSE::GameObject* object;
    Pong.Initialize("Pong!");
    object = Pong.Objects().ObjectCreate<SSE::GameObject>();
    while (!Pong.bQuit)
    {
        Pong.Update();
        Pong.Draw();
    }
    return 0;
}

This is where I call the function. Game is a class that runs the behind the scenes work for me (everything's fine with that class), Game.Objects() returns the Game's ObjectManager which is in charge of creating and deleting objects as well as giving objects their components. ObjectCreate is a template function which returns a pointer to the new object created.

From the ObjectManager's .cpp file:

template <class G>G* ObjectManager::ObjectCreate()
{
    ObjectList* tempObjList;
    tempObjList = new tempObjList();
    tempObjList->objectType = G->ClassName();
    tempObjList->objectTypeNumber = 0;
    for (unsigned int i = 0; i < v_objList.size(); i++;)
    {
        if (v_objList[i]->objectType == tempObjList->objectType)
            tempObjList->objectTypeNumber++;
    }
    tempObjList->gameObject = new G(tempObjList->objectType + "_" + tempObjList->objectTypeNumber);
    v_objList.push_back(tempObjList);
    if (v_objList.back() != tempObjList)
    {
        delete tempObjList;
        return NULL;
    }
    return v_objList.back();
}

This assigns a unique name for the new GameObject and creates it in memory, then stores it into a vector. One other thing to mention is I've been getting this unresolved external symbol error for many of the ObjectManager and GameObject functions similar to this one, but just the same only if I call them in code.

Just for reference, the error is: Error 2 error LNK2019: unresolved external symbol "public: class SSE::GameObject * __thiscall SSE::ObjectManager::ObjectCreate(void)" (??$ObjectCreate@VGameObject@SSE@@@ObjectManager@SSE@@QAEPAVGameObject@1@XZ) referenced in function _SDL_main C:\SDL\SimpleStateEngine\SSE\main.obj SSE

Let me know if you need anything else, I've been searching for hours.

  • Assuming that what's being said there is my problem, should I take out the templated functions in my GameObject class and make the GameObject class itself a template class? – Myles Salholm Mar 13 '13 at 02:53
  • See my answer. You'll need to define it in some header included before `_tmain`. – Drew Dormann Mar 13 '13 at 02:55
  • The design is okay. Just move the definition `template G* ObjectManager::ObjectCreate()` from ObjectManager.cpp to ObjectManager.h. – aschepler Mar 13 '13 at 13:10

1 Answers1

0

You've fallen into the common trap of defining a template function in a cpp file.

You need to define the function anywhere the calling code can see it, because it's only instantiated when it's used with specific template parameters.

Your other option is to instantiate it specifically as ObjectCreate<SSE::GameObject> in your cpp file.

What you've done is define how it could be instantiated in ObjectManager.cpp and called it as if it were instantiated in another cpp file.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • I'm not the keenest on C++ as it's been a few years since I've used it, but what I'm trying to do is make a function that will create an object for the user and put it into some sort of array. I want the user to be able to define their own objects based off of GameObject and yet have ObjectCreate still function with their child class. Should I even be using templates? – Myles Salholm Mar 13 '13 at 03:07