Question

I have been stumped all day on this problem. Basically I want to check to confirm the template type inherits from a class that will work for what I'm doing. To do this I used std::enable_if, and I have something that works the way I want it to at compile time, but I keep getting liker undefined reference errors when I try to actually do something with the class.

class TrainingCompare
{
public:
    virtual bool operator() (PUnitType& lhs, PUnitType& rhs)=0;
};

// The following Magic will result in a type error if T does not inherit from TrainingCompare
template<class T, class Enable = void>
class TrainAction;

template <class T>
class TrainAction<T,
    typename std::enable_if< std::is_base_of< TrainingCompare, T >::value>::type>
    : public BasicAction
{
    priority_queue<UnitType, vector<PUnitType>, T> trainQueue;
public:
    TrainAction();
    void addUnitRequest(UnitType uType, int priority = 0);
};

To get the source file to compile was a challenge but what I came up with looked like total crap to have a lot of so I made a macro to shrink the code.

#include "TrainAction.h"

#define Magic(rtype) template <class T>\
    rtype TrainAction<T, typename std::enable_if< std::is_base_of< TrainingCompare, T >::value>::type>

template<class T>
TrainAction<T, typename std::enable_if< std::is_base_of< TrainingCompare, T >::value>::type>::TrainAction()
{
    group = NULL;
}

Magic(void)::addUnitRequest(UnitType uType, int priority = 0)
{
    trainQueue.push(PUnitType(UnitType));
}

I have the feeling that my macro is wrong but my header is right, but that's just a hunch. Thanks in advance.

1>ExampleAIModule.obj : error LNK2001: unresolved external symbol "public: void __thiscall TrainAction<class TrainComp,void>::addUnitRequest(class BWAPI::UnitType,int)" (?addUnitRequest@?$TrainAction@VTrainComp@@X@@QAEXVUnitType@BWAPI@@H@Z)
1>ExampleAIModule.obj : error LNK2001: unresolved external symbol "public: __thiscall TrainAction<class TrainComp,void>::TrainAction<class TrainComp,void>(void)" (??0?$TrainAction@VTrainComp@@X@@QAE@XZ)
1>C:\bwapi4\broodwarbot\branches\DR_BWAIModule\Release\ExampleAIModule.dll : fatal error LNK1120: 2 unresolved externals
Was it helpful?

Solution

The problem is that all template code must be in the headers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top