c++ - 在 C++ 中创建一个用于不断计算数学函数的 dll

标签 c++ dll

我遇到了一个问题。我正在尝试创建一个 dll,当我发送 2 组不同的值时,它可以计算 2 点之间的距离。 但是,当我发送第二组值时,我意识到我的第一组值在数组中丢失了(数组用于存储值)

下面是我的代码:

int click = 0;   //click is used to measure the number of times i have clicked, ie to say the number of times im injecting new (a,b) points into the function below.

double MeasureDistance(double a, double b)
{
    /******* Create Array to Store The Points ********/

    /** Initializing the array **/
    double xDistance =0;
    double yDistance =0;
    double TDistance = 0;
    static double **Array;
    int column = 0; //used to toggle the column number
    int width = 100;
    int height = 100;
    Array = new double *[width];
    for (int i=0; i <width; i++)
    {
        Array [i] = new double [height];
    }

    /*** Now a and b are stored inside the Array[0][0] ***/

    for (column =0; column <2; column ++)
    {
        if ((column % 2)==0)
        {
            Array [click][column] = a; //storing at [0,0]
        }
        else 
        {   
            Array [click][column] = b; //storing at [0,1]
        }
    }
                for (int row = 2; row < click; row ++)
    {
        for (column = 0; column <2; column ++)
        {   
            if ((column % 2) == 0)
            {
                xDistance = Array [0][column] - Array [row][column];
            }
            else
            {
                yDistance = Array [0][column] - Array [row][column];
            }
        }

        TDistance = sqrt((xDistance * xDistance) + (yDistance * yDistance));
    }

/*** Clearing up of array ***/
    for (int i = 0; i < width; i++)
    {
        delete[] Array[i];
    }
    delete[] Array;


click++;
    return TDistance ;
}

我意识到,当我注入(inject)第二组 a 和 b 值时,数组 [0][0] 和 [0][1] 中的值丢失了,但我的第二组值存储在 [1] 中[0] 和 [1][1]。知道如何在不丢失以前的值的情况下运行此脚本吗? 提前致谢。编辑代码以清除一些查询。

最佳答案

使用 Array = new double *[width]; 行,您将在每个函数调用中初始化您的数组。如果您需要存储值(我非常怀疑),最好使用静态初始化 vector 。但一般来说,让函数的结果依赖于之前的调用是一个非常糟糕的主意。如果你真的需要积累状态,考虑为此目的创建一个函数对象。

编辑:

使用函数对象,您可以通过更改 operator() 和数据结构来更改算法的行为,以通过成员变量保存数据。

第二次编辑: 你可能想要这样的东西:

struct MeasureDistance {
  double last_x;
  double last_y;

  MeasureDistance() : last_x(0), last_y(0) {}
  double operator()(double new_x, double new_y) {
    double diff_x=last_x-new_x;
    double diff_y=last_y-new_y;
    double result=sqrt(diff_x*diff_x,diff_y*_diff_y);

    last_x=new_x;
    last_y=new_y;

    return result;
};

MeasureDistance md;
cout 
  << md(0.0, 1.0) << '\n' //prints 1
  << md(2.0, 1.0) << '\n' //prints 2
  ;

关于c++ - 在 C++ 中创建一个用于不断计算数学函数的 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17186294/

相关文章:

c++ - FPS 风格相机

c++ - 在 Visual Studio 6 中从 VB 调用 VS2010 C++ dll

windows - sox 失败实用程序 : Unable to load MAD decoder library (libmad) function "mad_stream_buffer"

c++ - 在图上实现 BFS

c++ - 为什么 std::pair 在赋值中调用显式构造函数

c# - 为什么在 DLL 文件中启动的进程在使用控制台应用程序进行测试时有效,但在被另一个 DLL 文件调用时却无效?

c++:不允许定义 dllimport 函数,使用 visual studio 2010 构建

c# - 在 C# 中使用 EHLAPI32 和 RUMBA 作为仿真供应商

c++ - 访问 tuple_t 的类型

c++ - 代理项目中的慢套接字读取()