0

I'm trying to create a pool to manage the allocation and so on of little game objects that will die and reborn quickly.

For it I have created a Pool:

template <class T>
class Pool {

public:

    T* obtain()
    {
        T* obj = 0;

        if (_avaibles.size() > 0)
        {
            std::vector<T*>::iterator it = _avaibles.begin();
            obj = *it;
            _avaibles.erase(it);
        }
        else
            obj = new T();

        return obj;
    }

    void free(T* obj)
    {
        _avaibles.push_back(obj);
    }

    void clear()
    {
        std::vector<T*>::iterator it = _avaibles.begin();

        while (it != _avaibles.end())
        {
            T act = *it;
            delete act;
            ++it;
        }
    }

private:

    std::vector<T*> _avaibles;

};

The problem is I'm getting unresolved external symbols. The pool is placed as a static member of a class:

 static Pool<Ship> _shipPool;

Here is the error:

Error   16  error LNK2001: unresolved external symbol "private: 
static class Pool<class Ship> Asdf::_shipPool"  
(?_shipPool@Asdf@@0V?$Pool@VShip@@@@A)  C:\-\Asdf.obj
David
  • 15,894
  • 22
  • 55
  • 66
Frion3L
  • 1,502
  • 4
  • 24
  • 34
  • Please provide the exact wording of the error message you are getting (if it is a stack/core dump, we just need the relevant bits). – Robert Harvey Feb 16 '14 at 16:40
  • the title of the question and the issue raised are very different from each other... – UmNyobe Feb 17 '14 at 07:44

1 Answers1

2

You can't split up a template like that. Put the implementation into the .hpp file and everything's shiny.

Refer to Why can templates only be implemented in the header file? for futher information.

Community
  • 1
  • 1
filmor
  • 30,840
  • 6
  • 50
  • 48
  • You are right. Now is giving me this: cannot convert from Ship** to Ship* – Frion3L Feb 16 '14 at 16:46
  • That's because you're interface is not right. As an exercise, do the template instantiation once on paper to see which types you have for your variables. Hint: `new T` is `new Ship*` in your case. You might also want to head over to http://codereview.stackexchange.com since there are more flaws in your implementation. – filmor Feb 16 '14 at 16:50
  • @filmor: Code Review will only work when the code functions. – Robert Harvey Feb 16 '14 at 16:58
  • Thanks again. I fixed that but still, now it cannot resolve the "external symbol private: static class Pool ..." – Frion3L Feb 16 '14 at 16:59
  • What did you fix? You have moved the whole implementation into the header file? Would you please refresh the question or post a new one with your current code? – filmor Feb 16 '14 at 17:07
  • Ah, well now you have the problem the other way round. If you have a static member of a class you have to *define* it in exactly one implementation file. So in `asdf.cpp` add a line `static Pool Asdf::_shipPool;`. – filmor Feb 16 '14 at 17:45