expression必须有指向函数类型的指针

时间:2012-11-22 06:37:22

标签: c++ function pointers reference

我对C ++很新,我正在尝试学习如何使用指针。我有以下文件创建坐标,然后使用随机数生成器随机方向移动它们。

值sigmaf_point从文本文件输入:

 void methane_coords(double *&sigmaf_point)

 double dummy_int = 1;
 string dummystring;
        string s;

        ifstream Dfile;
        std::stringstream out;

        out << 1;
        s = out.str() + ".TXT";
        Dfile.open (s.c_str());

        if (Dfile.fail())
        {
            return;
        }

        for (int i=0; i<dummy_int; i++)
        {
        Dfile >> sigmaf_point[i];
        }

然后我在另一个函数中使用它:

double initial_energy(double **coords_fluid, const double *box_size){


// Loop over all pairs of atoms and calculate the LJ energy
double total_energy = 0;

for (int i = 0; i <= n_atoms-1; i++)
{   

        sf1=sigmaf_point(coords_fluid[i][3]);
        ef1=epsilonf_point(coords_fluid[i][3]);

        // Energy fluid-fluid
        for (int j = i+1; j <= n_atoms-1; j++)
        {               
            sf2=sigmaf_point(coords_fluid[j][3]);
            ef2=epsilonf_point(coords_fluid[j][3]);             

            double delta_x = coords_fluid[j][0] - coords_fluid[i][0];
            double delta_y = coords_fluid[j][1] - coords_fluid[i][1];
            double delta_z = coords_fluid[j][2] - coords_fluid[i][2];

            // Apply periodic boundaries
            delta_x = make_periodic(delta_x, box_size[0]);
            delta_y = make_periodic(delta_y, box_size[1]);
            delta_z = make_periodic(delta_z, box_size[2]);

            // Calculate the LJ potential
            s=(sf1+sf2)/2.0;
            e=pow((ef1*ef2),0.5);
            double r = pow((delta_x*delta_x) + (delta_y*delta_y) +
                      (delta_z*delta_z),0.5)/s;

        double e_lj = 4*((1/pow(r,12.0))-(1/pow(r,6.0))/e);

        total_energy = (total_energy + e_lj);
        }
    }

coords_fluid在主文件中创建如下:

double **coords_fluid = new double*[5000];

现在问题出在sf1 = sigmaf_point(coords_fluid [i] [3]);

我为sigmaf_point收到错误“表达式必须有指向函数类型的指针”。我对此有点困惑,我知道这是关于我如何调用变量但似乎无法修复它。

干杯

1 个答案:

答案 0 :(得分:0)

首先:对指针的重新引用是完全无用的,因为指针已经是一种引用。 因此,将double *&更改为double *double &。它会更快。

此外,我发现您使用sigmaf_point作为函数和数组。 哪一个? 你能否发表sigmaf_point的声明?

假设这是一个数组更改

sf1 = sigmaf_point(coords_fluid[i][3]);

sf1 = sigmaf_point[coords_fluid[i][3]];
相关问题