我有以下代码:
template<typename T> void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen) {
int nD = 0;
// if T is of type std::pair<int,int> then set no. of dimensions to 2
if (typeid(T) == typeid(std::pair<int, int>)) {
nD = 2;
}
// else if T is of type RWM::Triple<int,int,int> then set no. of dimensions to 3
else if (typeid(T) == typeid(RandomWalkMethods::Triple<int, int, int>)) {
nD = 3;
}
else {
return;
}
// Create vector of T structs to store DLA structure results
std::vector<T> aggResults;
// Initialise particle spawning type and attractor type for DLA system
RandomWalkMethods::ParticleSpawnType spawn = RandomWalkMethods::CONSTANT_RANDOM_BOUNDINGBOX_EDGE;
RandomWalkMethods::AttractorDLAType attractor = RandomWalkMethods::POINT;
// Under-estimate for fractal dimension of the DLA
const double fractalDimUnderestimateRecip = 1 / 1.65;
for (int i = 100; i <= 1000; i += 100) {
// initialise spawnDiameter using: exp(log(n)/fDUR) = n^{1/fDUR}
int spawnDiam = 2*static_cast<int>(std::pow(i, fractalDimUnderestimateRecip));
// if system is 2-dimensional, compute DLA for 2D on given lattice
if (nD == 2) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
// else if system is 3 dimensional, compute DLA for 3D on given lattice
else if (nD == 3) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
// compute the minimum bounding radius which encloses all particles in the DLA structure
double boundingRadius = std::sqrt(maxMagnitudeVectorOfMultiples< double, T >(aggResults));
}
}
我可以用一个声明来调用它,例如
computeFractalDimensionData< std::pair<int,int> >(lattice, randNumGen);
或者
computeFractalDimensionData< RandomWalkMethods::Triple<int,int,int> >(lattice, randNumGen);
whereTriple
只是一个我用 3 个元素定义的结构(本质上与 3 个字段相同std::pair
但扩展为 3 个字段)。此外,分别是和的函数diffusionLimitedAggregateRandomWalk2D
和diffusionLimitedAggregateRandomWalk3D
返回类型。std::vector<std::pair<int,int>>
std::vector<Triple<int,int,int>>
问题是,当我使用上述任一语句调用时,我收到以下错误(发生在赋值语句中aggResults = ...
):
binary '=': no operator found which takes a right-hand operand of type 'std::vector<std::pair<int,int>,std::allocator<_Ty>>' (or there is no acceptable conversion)
和类似的情况Triple<int,int,int>
。据我了解,这意味着我需要为这两个结构使用重载的赋值运算符 - 但是我认为这不是问题所在,因为之前在我的程序中正确使用了以下语句:
std::vector< std::pair<int,int> > aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(nParticles, boundingBox, spawnDiam, latticeType, randNumGen, attractor, &diffLimAggFile);
所以我知道我可以将 DLA 方法的结果分配给正确类型的变量,但是如果我通过使用将类型传递给模板函数来尝试它,编译器会抱怨,如上所示。
这里发生了什么,我将如何解决这个问题?