0

我有以下代码:

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 个字段)。此外,分别是和的函数diffusionLimitedAggregateRandomWalk2DdiffusionLimitedAggregateRandomWalk3D返回类型。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 方法的结果分配给正确类型的变量,但是如果我通过使用将类型传递给模板函数来尝试它,编译器会抱怨,如上所示。

这里发生了什么,我将如何解决这个问题?

4

2 回答 2

3

这是因为

aggResults = diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);

with aggResultsbeing astd::vector<T>被编译,即使TisTriple<int, int, int>diffusionLimitedAggregateRandomWalk2D返回 a std::vector<std::pair<int, int>>


建议的解决方案:声明一个模板化的函数并专门针对一些T

template<typename T>
void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen);

template<>
void computeFractalDimensionData<std::pair<int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}

template<>
void computeFractalDimensionData<Triple<int, int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
    // ...
}

它使代码更具可读性,并且无法编译以下行并出现帮助编译错误:

computeFractalDimensionData<void>(lattice, randNumGen);
于 2015-12-28T14:31:47.923 回答
2

YSC的解决方案很好。我想让您注意,您的函数中的以下代码是对模板的错误使用:

    // 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);
    }

模板用于静态多态性,并且您if (nd == ...)在模板函数中使用动态代码(这些)。正确使用静态多态性可以引入模板参数dimension

于 2015-12-28T14:45:24.980 回答