views:

56

answers:

1

Header

class linkNode {
    public:
        linkNode(void *p) {
            before = 0;
            after = 0;
            me = p;
        }

        linkNode *before;
        void *me;
        linkNode *after;
    };

    template <class T>
    class list
    {
    public:
        list(void) { first = last = NULL; size = 0; }
        ~list(void) { while(first) deleteNode(first); }      
    private:
        void deleteNode(linkNode *l);

        linkNode *first, *last;
        unsigned int size;
    };

.Cpp

template <class T>
inline void list<T>::deleteNode(linkNode *l) {
    if(c->before)
        if(c->after) {
            c->before->after = c->after;
            c->after->before = c->before;
        } else
            c->before->after = last = NULL;
    else
        if(c->after)
            c->after = first = NULL;
    delete c; size--;
}

I have this set to build as a .lib and it builds fine. When I try ePhys::list<int> myList; I get a linker error saying it cant find ePhys::list<int>::deleteNode(class ePhys::linkNode *) This is not a problem with setting up using the library, i have tested with other dummy classes.

I am using MSVC 2010 beta.

Is there any way to get this to link properly?

+4  A: 

C++ does not really support the separate compilation of templates - you need to put all your template code in the header file(s).

anon
A common way of organising template code is to put the class declaration in `foo.h` as normal, and the implementation in e.g. `foo.hxx` (suffix varies between projects). Right at the bottom of `foo.h` - but still inside the include guard - the implementation is then included using a `#include "foo.hxx"` directive.
Gareth Stockwell
@ gareth.stockwell So common that I've never seen it. What advantages does that give you?
anon
@Neil Butterworth: The idea is to separate the implementation from the interface. Try to hide the implementation details from the user.
Andy
@Andy Which you can't do, so it seems pointless to me.
anon