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 つの要素で定義した構造体です (本質的には と同じ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>。私が理解していることから、これは、これら2つの構造体にオーバーロードされた代入演算子が必要であることを意味します-ただし、次のステートメントは私のプログラムで以前に正しく使用されているため、ここでは問題ではないと思います:

std::vector< std::pair<int,int> > aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(nParticles, boundingBox, spawnDiam, latticeType, randNumGen, attractor, &diffLimAggFile);

したがって、DLA メソッドの結果を正しい型の変数に割り当てることができることはわかっていますが、上記のように型をテンプレート関数に渡して試してみると、コンパイラは文句を言います。

ここで何が起こっていて、この問題を解決するにはどうすればよいですか?

4

2 に答える 2

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 に答える