由指针和函数交互引起的C ++错误

时间:2015-05-18 16:43:24

标签: c++ pointers compiler-errors

我只使用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;
    }

1 个答案:

答案 0 :(得分:4)

比较签名:

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*等等。