gpt4 book ai didi

由于指针和函数的交互导致的 C++ 错误

转载 作者:行者123 更新时间:2023-11-27 22:58:21 27 4
gpt4 key购买 nike

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

最佳答案

比较签名:

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

关于由于指针和函数的交互导致的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30308694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com