Image

Imagealmound wrote in Imagecpp 😡aggravated

The problem with the slow deletes seems to have been solved. The speed problem actually seemed to come from running it in the IDE, but in fixing it I got some good tips for memory allocation, so thanks all for that.

But, in the last few days, and even uglier monster reared its head and that's linker errors from explicit template specialization :s I've worked on projects with templates before, but this is the first time I've tried to package a library with templates in it, and my specializations seem to be causing multiple definition errors. First off, anyone know how to specialize individual member functions without specializing the entire class?

my case boils down to something like the case below:



//VirtualParent.h
#ifndef __VIRTUAL_PARENT
#define __VIRTUAL_PARENT

template<typename T>
class VirtualParent {
public:
    VirtualParent() {};
    virtual void virt() = 0;
    virtual void toBeSpecialized();
};

template<typename T>
void VirtualParent<T>::toBeSpecialized() {
    /* ...general case code... */
}

#endif


//TemplateClass.h
#ifndef __TEMPLATE_CLASS
#define __TEMPLATE_CLASS

#include "VirtualParent.h"

template<typename T>
class TemplateClass : public VirtualParent {
public:
    TemplateClass() {};
    void virt();
    void toBeSpecialized();
    
    //other members...
}

template<typename T>
void TemplateClass<T>::virt() {
    /* ... */
}

//Want to do something like this
template<>
TemplateClass<int>::toBeSpecialized() {
    /* ...specialized code... */
}

#endif


//src1.cpp
#include "TemplateClass.h"

//src2.cpp
#include "TemplateClass.h"



That's what I'm doing right now, and I have a pretty good feeling it's pretty wrong (i.e. I should specialize the whole class), but the compiler accepts it, and the linker accepts it for the Test Harness, and rejects it for the main project. I don't want to do the full specialization if I can avoid it, because I don't want to have to provide an implementation of virt (in the example above) for each specialization, and there are many pure virtual functions in the actual case, and a good number of specializations. Any suggestions?


If anyone knows anything about VC7's support for template specialization and can help me, I'll post more specific code. Thanks.

[EDIT]
The classes above are hypothetically packaged in a library (.LIB) file, and a separate project (either the test harness project, or the main project) uses and links them, #including the .h files where they are used. Inclusion guards are also used for all the classes.
[/EDIT]

[EDIT2]
I changed the code to show the nature of the actual error. If I #include the template class in more than one .cpp file, the linker gives the multi-definition errors, even with the inclusion guards or #pragma once. Something like "public: void TemplateClass<int>::toBeSpecialized(void) ... has already been defined in src1.obj".
[/EDIT2]