链接器错误,未定义对模板化构造函数的引用
Linker error with undefined reference to templated constructor
我正在尝试创建模板化函数,该函数将T类型的向量作为输入并将其划分为多个线程。这是通过构建模板化类型为 T "VectorWorker"的对象来完成的,这些对象带有迭代器到他们需要处理的数据的开头和结尾。
我遇到了一个链接器错误,其中模板化的 std::vec::const_iterators 我试图传递给 VectorWorker 构造函数找不到相应的构造函数。我的理解是,我用来调用构造函数的参数类型与我定义的构造函数中的参数类型不同。
构造 VectorWorker 的模板化函数的相关行是:
/* function to create initial worker that spawns more workers*/
template <typename T>
void makeWorkers(const std::vector<T>& initializerVector)
{
int quotient = initializerVector.size()/NUMTHREADS; // divide by number of threads
int remainder = initializerVector.size()%NUMTHREADS; //find remainder after division which will have to be distributed
//store the iterators for all our workers in a 2 X NUMTHREADS matrix. Where first column is each workers beginning iterator
//and second column is each worker's ending iterator
typename std::vector<T>::const_iterator itArray[NUMTHREADS][2];
VectorWorker<T> workerArray [NUMTHREADS]; //instantiate array to hold all workers
std::thread threadArray[NUMTHREADS]; //instantiate array to hold threads which will run worker's work() method
//The first "remainder" distances should be quotient+1, the remaining should just be quotient to yield
// remainder(quotient+1)+(N-remainder)(quotient) = N*Quotient+remainder
itArray[0][0] = initializerVector.begin();
if(remainder ==0)
{
remainder=NUMTHREADS; //to prevent division by zero
itArray[0][1] = initializerVector.cbegin()+quotient-1; //limit distance between first two iterators to quotient
}
else
itArray[0][1] = initializerVector.cbegin()+quotient-1; //The distance between first two iterators should be quotient+1
for (int i=1; i<NUMTHREADS; ++i)
{
itArray[i][0]=itArray[i-1][0]+quotient+1-(int)(i<remainder);
itArray[i][1]=itArray[i-1][1]+quotient+1-(int)(i<remainder);
}
/*
typename std::vector<T>::const_iterator begin;
begin = itArray[0][0];
*/
// construct all worker objects
for( int i=0; i<NUMTHREADS; ++i)
{
workerArray[i]=VectorWorker<T>(itArray[i][0],itArray[i][1]); //copy assignment into default initialized array objects
}
VectorWorker 的模板化类定义为:
template <typename T>
class VectorWorker
{
public:
VectorWorker<T>() = default;
VectorWorker<T>( typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end);
void Work() const
{
std::cout<<"I worked"<<std::endl;
return;
};
private:
typename std::vector<T>::const_iterator beginIt; /* Stores value of left iterator that defines data for this worker */
typename std::vector<T>::const_iterator endIt; /* Stores value of right iterator that defines data for this worker */
};
最后,我收到的链接器错误是:
CMakeFiles/P2.dir/fft2d.cc.o: In function `void makeWorkers<Complex*>(std::vector<Complex*, std::allocator<Complex*> > const&)':
undefined reference to `VectorWorker<Complex*>::VectorWorker(__gnu_cxx::__normal_iterator<Complex* const*, std::vector<Complex*, std::allocator<Complex*> > >, __gnu_cxx::__normal_iterator<Complex* const*, std::vector<Complex*, std::allocator<Complex*> > >)'
编辑:产生错误的MWE:https://www.onlinegdb.com/online_c++_compiler
#include <vector>
#include <thread>
#define NUMTHREADS 4
template <typename T>
class VectorWorker
{
public:
VectorWorker<T>() = default;
VectorWorker<T>( typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end);
void Work() const
{
std::cout<<"I worked"<<std::endl;
return;
};
private:
typename std::vector<T>::const_iterator beginIt; /* Stores value of left iterator that defines data for this worker */
typename std::vector<T>::const_iterator endIt; /* Stores value of right iterator that defines data for this worker */
};
using namespace std;
/* function to create initial worker that spawns more workers*/
template <typename T>
void makeWorkers(const std::vector<T>& initializerVector)
{
int quotient = initializerVector.size()/NUMTHREADS; // divide by number of threads
int remainder = initializerVector.size()%NUMTHREADS; //find remainder after division which will have to be distributed
//store the iterators for all our workers in a 2 X NUMTHREADS matrix. Where first column is each workers beginning iterator
//and second column is each worker's ending iterator
typename std::vector<T>::const_iterator itArray[NUMTHREADS][2];
VectorWorker<T> workerArray [NUMTHREADS]; //instantiate array to hold all workers
std::thread threadArray[NUMTHREADS]; //instantiate array to hold threads which will run worker's work() method
//The first "remainder" distances should be quotient+1, the remaining should just be quotient to yield
// remainder(quotient+1)+(N-remainder)(quotient) = N*Quotient+remainder
itArray[0][0] = initializerVector.begin();
if(remainder ==0)
{
remainder=NUMTHREADS; //to prevent division by zero
itArray[0][1] = initializerVector.cbegin()+quotient-1; //limit distance between first two iterators to quotient
}
else
itArray[0][1] = initializerVector.cbegin()+quotient-1; //The distance between first two iterators should be quotient+1
for (int i=1; i<NUMTHREADS; ++i)
{
itArray[i][0]=itArray[i-1][0]+quotient+1-(int)(i<remainder);
itArray[i][1]=itArray[i-1][1]+quotient+1-(int)(i<remainder);
}
/*
typename std::vector<T>::const_iterator begin;
begin = itArray[0][0];
*/
// construct all worker objects
for( int i=0; i<NUMTHREADS; ++i)
{
workerArray[i]=VectorWorker<T>(itArray[i][0],itArray[i][1]); //copy assignment into default initialized array objects
}
return;
}
int main()
{
cout<<"Hello World";
std::vector<int*> testVec = {new int*, new int*};
makeWorkers(testVec);
return 0;
}
你声明了VectorWorker
的第二个构造函数,但你从未实现过它。
#include <vector>
#include <thread>
#include <iostream>
using namespace std;
template <typename T>
class VectorWorker
{
public:
VectorWorker<T>( typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end)
{
I NEED IMPLEMENTATION <-------------------------
}
};
template <typename T>
void makeWorkers(const std::vector<T>& initializerVector)
{
typename std::vector<T>::const_iterator a,b;
VectorWorker<T>(a,b);
}
int main()
{
std::vector<int*> testVec = {new int*, new int*};
makeWorkers(testVec);
return 0;
}
相关文章:
- 在类构造函数中传递对外部函数的引用
- 在 c++ 中将变量作为结构构造函数中的引用传递
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 移动构造函数和右值引用
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- 为什么我需要在转换构造函数上引用 this->?
- 关于隐式声明的复制构造函数的引用在逻辑上不清楚
- C++:右值引用构造函数和复制省略
- CLion - 无法解决类中对构造函数的未定义引用
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- 调用默认构造函数时不引用它
- C++ 构造函数传递对基类对象的引用
- 使用默认构造函数引用成员变量初始化错误
- 构造函数引用参数导致seg错误
- 未定义的类构造函数引用,当它被正确定义时
- 如何从父类构造函数引用重写的静态方法
- 创建构造函数引用全局变量的类的全局实例时,C++ 程序崩溃