在C ++中使用模板的新知识。我想根据将单个对象指针传递给函数还是对象指针的std :: vector来创建类函数的模板专用化。我希望std :: vector通过引用传递。我不太清楚所需的语法:
StateEvolver.h
#ifndef STATEEVOLVER_H
#define STATEEVOLVER_H
#include "Vehicle.hh"
class stateevolver
{
public:
stateevolver();
template<typename T> void evolveState(T, double time);
};
// Template specialization declarations for functions using either std::vector<vehicle*> or single vehicle pointer
template<>
void stateevolver::evolveState<std::vector<vehicle*>&>(std::vector<vehicle*> & , double time);
template<>
void stateevolver::evolveState<vehicle*>(vehicle*, double time);
#endif
StateEvolver.cpp
template<>
void stateevolver::evolveState<std::vector<vehicle*>&>(std::vector<vehicle*> & vehicleVec, double time) {
}
我将其实例化到另一个文件中,如下所示:
MyStateEvolver.evolveState(vectorOfVehiclePointers, time); // COMPILE ERROR
并收到此编译时错误:
undefined reference to `void stateevolver::evolveState<std::vector<vehicle*, std::allocator<vehicle*> > >(std::vector<vehicle*, std::allocator<vehicle*> >, double)'
有什么想法我要去哪里吗?谢谢。