Неопределенная ссылка для функции шаблона в C++
У меня есть template
функция, и у меня всегда есть ошибка, используя typename
в моих переменных. У меня есть две функции для вызова, они
template <>
void caffe_axpy<float>(const int N, const float alpha, const float* X,
float* Y) { cblas_saxpy(N, alpha, X, 1, Y, 1); }
template <>
void caffe_axpy<double>(const int N, const double alpha, const double* X,
double* Y) { cblas_daxpy(N, alpha, X, 1, Y, 1); }
Так как они могут быть двойными или плавающими, то я сделал функцию шаблона как
template <typename Dtype>
void Blob<Dtype>::UpdateNew(Dtype decay, Dtype lr) {
// We will perform update based on where the data is located.
switch (data_->head()) {
case SyncedMemory::HEAD_AT_CPU:
// perform computation on CPU
//this is using data and decay to update diff
caffe_axpy<Dtype>(count_, (Dtype)decay,
static_cast<const Dtype*>(data_->cpu_data()),
static_cast<Dtype*>(diff_->mutable_cpu_data()));
//this is using diff and lr to update data
caffe_axpy<Dtype>(count_, (Dtype)lr,
static_cast<const Dtype*>(diff_->cpu_data()),
static_cast<Dtype*>(data_->mutable_cpu_data()));
break;
case SyncedMemory::HEAD_AT_GPU:
case SyncedMemory::SYNCED:
#ifndef CPU_ONLY
// perform computation on GPU
//this is using data and decay to update diff
caffe_gpu_axpy<Dtype>(count_, (Dtype)decay,
static_cast<const Dtype*>(data_->gpu_data()),
static_cast<Dtype*>(diff_->mutable_gpu_data()));
//this is using diff and lr to update data
caffe_gpu_axpy<Dtype>(count_, (float)lr,
static_cast<const Dtype*>(diff_->gpu_data()),
static_cast<Dtype*>(data_->mutable_gpu_data()));
#else
NO_GPU;
#endif
break;
default:
LOG(FATAL) << "Syncedmem not initialized.";
}
}
Если я изменю все следующие строки с Dtype на float, то ошибки не будет. Если я использую Dtype, то у меня есть неопределенные ошибки ссылки.
caffe_axpy<float>(count_, (float)decay,
static_cast<const Dtype*>(data_->cpu_data()),
static_cast<float*>(diff_->mutable_cpu_data()));
//this is using diff and lr to update data
caffe_axpy<float>(count_, (float)lr,
static_cast<const float*>(diff_->cpu_data()),
static_cast<float*>(data_->mutable_cpu_data()));
caffe_gpu_axpy<float>(count_, (float)decay,
static_cast<const float*>(data_->gpu_data()),
static_cast<float*>(diff_->mutable_gpu_data()));
//this is using diff and lr to update data
caffe_gpu_axpy<float>(count_, (float)lr,
static_cast<const float*>(diff_->gpu_data()),
static_cast<float*>(data_->mutable_gpu_data()));
Что может быть не так?
РЕДАКТИРОВАТЬ: ошибка компиляции
debug_debug/lib/libcaffe.so: undefined reference to `void caffe::caffe_axpy<unsigned int>(int, unsigned int, unsigned int const*, unsigned int*)'
.debug_debug/lib/libcaffe.so: undefined reference to `void caffe::caffe_gpu_axpy<unsigned int>(int, unsigned int, unsigned int const*, unsigned int*)'
.debug_debug/lib/libcaffe.so: undefined reference to `void caffe::caffe_gpu_axpy<int>(int, int, int const*, int*)'
.debug_debug/lib/libcaffe.so: undefined reference to `void caffe::caffe_axpy<int>(int, int, int const*, int*)'
collect2: error: ld returned 1 exit status