【问题标题】:C++ Error Due to Interaction of Pointer and Function由于指针和函数的交互导致的 C++ 错误
【发布时间】:2015-05-18 16:43:24
【问题描述】:

我只使用 C++ 大约 3 周,所以请耐心等待,因为这个问题的格式可能不正确(从数值分析开始可能不是一个好主意)。我认为这个问题与指针有关,而且可能很普通。我对指针有基本的了解,但似乎正在进行某种嵌套/交互。 g++ 给了我一个编译器错误:

In function ‘void test02()’:
minpackTest01.cpp:95:50: error: invalid conversion from ‘void (*)(int, double*, double*, int*)’ to ‘void (*)(int*, double*, double*, int*)’ [-fpermissive]
   info = hybrd1_ ( f02, n, x, fvec, tol, wa, lwa );
                                                  ^
minpackTest01.cpp:95:50: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
minpackTest01.cpp:95:50: error: cannot convert ‘double’ to ‘double*’ for argument ‘5’ to ‘void hybrd1_(void (*)(int*, double*, double*, int*), int*, double*, double*, double*, int*, double*, int*)’

标题中的函数显示为

/* find a zero of a system of N nonlinear functions in N variables by
   a modification of the Powell hybrid method (Jacobian calculated by
   a forward-difference approximation) */
void hybrd1_ ( void (*fcn)(int *n, double *x, double *fvec, int *iflag ), 
           int *n, double *x, double *fvec, double *tol, int *info,
           double *wa, int *lwa );

我的主要代码在第 95 行“info = hybrd1_ ...”有问题

    #include <iostream>
    #include <armadillo>
    #include <cstdlib>
    #include <cmath>      //needed for exponent and GAMMA function
    #include <minpack.h>  //weird compiler call here

    using namespace std;
    using namespace arma;


    // function prototyping
    int main();
    void test02();
    void f02( int n, double x[], double fvec[], int *iflag );

    //****************************************************************************80
    int main ( )
    //****************************************************************************80
    //  Purpose:
    //    MAIN is the main program for MINPACK_PRB.
    //
    //  Discussion:
    //    MINPACK_PRB tests the MINPACK library.
    //
    {
      cout << "\n";
      cout << "MINPACK_PRB\n";
      cout << "  C++ version:\n";
      cout << "  Test the MINPACK library.\n";

      test02();

      cout << "\n";
      cout << "MINPACK_PRB\n";
      cout << "  Normal end of execution.\n";
      cout << "\n";

      time_t timer;
      cout << time(&timer) << endl;

      return 0;
    }

    //****************************************************************************80

    void test02 ( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests HYBRD1.
    //
    //  Discussion:
    //
    //    This is an example of what your main program would look
    //    like if you wanted to use MINPACK to solve N nonlinear equations
    //    in N unknowns.  In this version, we avoid computing the jacobian
    //    matrix, and request that MINPACK approximate it for us.
    //
    //    The set of nonlinear equations is:
    //
    //      x1 * x1 - 10 * x1 + x2 * x2 + 8 = 0
    //      x1 * x2 * x2 + x1 - 10 * x2 + 8 = 0
    //
    //    with solution x1 = x2 = 1
    //
    {
      double *fvec;  //output array of length n which contains the functions evaluated at the output x. 
      int iflag;     //...not quite sure on this one
      int info;      //integer output variable. If the user has terminated execution, info is set to the (negative) value of iflag.  Otherwise RTFM
      int lwa;       //length of work array
      int n = 2;     //number of unknowns (x1, x2)
      double tol = 0.00001;
      double *wa;    //work array of length lwa
      double *x;     //array of length n. On input x must contain an initial estimate of the solution vector. On output x contains the final estimate of the solution vector. 

      lwa = ( n * ( 3 * n + 13 ) ) / 2;
      fvec = new double[n];  //mathematical output
      wa = new double[lwa];
      x = new double[n];     //mathematical input

      cout << "\n";
      cout << "TEST02\n";
      cout << "  HYBRD1 solves a nonlinear system of equations.\n";

      x[0] = 3.0;  //initial x1
      x[1] = 0.0;  //initial x2
      cout << n << x << "  Initial X" << endl;
      iflag = 1;
      f02 ( n, x, fvec, &iflag );

      cout << n << fvec << "  F(X)" << endl;

      info = hybrd1_ ( f02, n, x, fvec, tol, wa, lwa );

      cout << "\n";
      cout << "  Returned value of INFO = " << info << "\n";
      cout <<  n << x << "  X" << endl;
      cout <<  n << fvec << "  F(X)" << endl;

      delete [] fvec;
      delete [] wa;
      delete [] x;

      return;
    }


#include <iostream>
#include <armadillo>
#include <cstdlib>
#include <cmath>      //needed for exponent and GAMMA function
#include <minpack.h>  //weird compiler call here

using namespace std;
using namespace arma;


// function prototyping
int main();
void test02();
void f02( int n, double x[], double fvec[], int *iflag );

//****************************************************************************80
int main ( )
//****************************************************************************80
//  Purpose:
//    MAIN is the main program for MINPACK_PRB.
//
//  Discussion:
//    MINPACK_PRB tests the MINPACK library.
//
{
  cout << "\n";
  cout << "MINPACK_PRB\n";
  cout << "  C++ version:\n";
  cout << "  Test the MINPACK library.\n";

  test02();

  cout << "\n";
  cout << "MINPACK_PRB\n";
  cout << "  Normal end of execution.\n";
  cout << "\n";

  time_t timer;
  cout << time(&timer) << endl;

  return 0;
}

//****************************************************************************80

void test02 ( )

//****************************************************************************80
//
//  Purpose:
//
//    TEST02 tests HYBRD1.
//
//  Discussion:
//
//    This is an example of what your main program would look
//    like if you wanted to use MINPACK to solve N nonlinear equations
//    in N unknowns.  In this version, we avoid computing the jacobian
//    matrix, and request that MINPACK approximate it for us.
//
//    The set of nonlinear equations is:
//
//      x1 * x1 - 10 * x1 + x2 * x2 + 8 = 0
//      x1 * x2 * x2 + x1 - 10 * x2 + 8 = 0
//
//    with solution x1 = x2 = 1
//
{
  double *fvec;  //output array of length n which contains the functions evaluated at the output x. 
  int iflag;     //...not quite sure on this one
  int info;      //integer output variable. If the user has terminated execution, info is set to the (negative) value of iflag.  Otherwise RTFM
  int lwa;       //length of work array
  int n = 2;     //number of unknowns (x1, x2)
  double tol = 0.00001;
  double *wa;    //work array of length lwa
  double *x;     //array of length n. On input x must contain an initial estimate of the solution vector. On output x contains the final estimate of the solution vector. 

  lwa = ( n * ( 3 * n + 13 ) ) / 2;
  fvec = new double[n];  //mathematical output
  wa = new double[lwa];
  x = new double[n];     //mathematical input

  cout << "\n";
  cout << "TEST02\n";
  cout << "  HYBRD1 solves a nonlinear system of equations.\n";

  x[0] = 3.0;  //initial x1
  x[1] = 0.0;  //initial x2
  cout << n << x << "  Initial X" << endl;
  iflag = 1;
  f02 ( n, x, fvec, &iflag );

  cout << n << fvec << "  F(X)" << endl;

  info = hybrd1_ ( f02, n, x, fvec, tol, wa, lwa );

  cout << "\n";
  cout << "  Returned value of INFO = " << info << "\n";
  cout <<  n << x << "  X" << endl;
  cout <<  n << fvec << "  F(X)" << endl;

  delete [] fvec;
  delete [] wa;
  delete [] x;

  return;
}

//****************************************************************************80
void f02 ( int n, double x[], double fvec[], int *iflag )
//****************************************************************************80
{

  //the entry-wise evaluation of the output array
  //algebra in terms of the input X vector
  fvec[0] = x[0] * x[0] - 10.0 * x[0] + x[1] * x[1] + 8.0;
  fvec[1] = x[0] * x[1] * x[1] + x[0] - 10.0 * x[1] + 8.0;

  return;
}//****************************************************************************80
    void f02 ( int n, double x[], double fvec[], int *iflag )
    //****************************************************************************80
    {

      //the entry-wise evaluation of the output array
      //algebra in terms of the input X vector
      fvec[0] = x[0] * x[0] - 10.0 * x[0] + x[1] * x[1] + 8.0;
      fvec[1] = x[0] * x[1] * x[1] + x[0] - 10.0 * x[1] + 8.0;

      return;
    }

【问题讨论】:

    标签: c++ pointers compiler-errors


    【解决方案1】:

    比较签名:

    void hybrd1_ ( void (*fcn)(int *n, double *x, double *fvec, int *iflag ), 
    //                         ^^^^^^
    //                         first arg is int*
    

    你正在传入:

    void f02( int n, double x[], double fvec[], int *iflag );
    //        ^^^^^
    //        first arg is int
    

    其他参数同样不对齐 - 您传递的是 int,而 hybrd1_ 需要 int*,等等。

    【讨论】:

    • 这就是区别,解决方法是什么?如果我在原型声明和 f02() 的完整声明中都输入“int *n”,错误仍然存​​在。
    • @e5haffer 解决方法是将正确的类型传递给您的函数。 (旁注,没有“原型声明”和“完整声明” - 有“声明”和“定义”)。
    • @e5haffer 使fnint* 作为第一个参数。
    猜你喜欢
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2015-07-13
    • 2015-12-11
    • 2018-05-24
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多