64 位平台上的编译器错误 C2664
Compiler error C2664 on 64 bit platform
我有一个Visual Studio2012年用win 32平台编译运行的程序,换平台到X64后报错如下:
" test_integration.cpp(27): error C2664: 'hcubature_v' : 无法将参数 2 从 'int (__cdecl *)(unsigned int,unsigned int,const double *,void *,unsigned int,double *)' 转换为 'integrand_v' "
有人可以提供一些建议吗?谢谢!
#include <iostream>
#include "cubature.h"
#include <ctime>
#include <limits>
using namespace std;
int f_v(unsigned ndim, unsigned npts, const double *x, void *fdata, unsigned fdim, double *fval) {
double sigma = *((double *) fdata);
unsigned i, j;
for (j = 0; j < npts; ++j) { // evaluate the integrand for npts points
double sum = 0;
for (i = 0; i < ndim; ++i) sum += x[j*ndim+i] * x[j*ndim+i];
fval[j] = exp(-sigma * sum);
};
return 0; // success
}
int main(){
double xmin[3] = {-2,-2,-2}, xmax[3] = {2,2,2}, sigma = 0.5;
double val_v,err_v, time_v;
const clock_t begin_time_v = clock();
for (int j = 0; j<1000;j++)
{
// hcubature_v calculates a multidimensional integration.
hcubature_v(1, f_v, &sigma, 1, xmin, xmax, 0, 0, 1e-4, ERROR_INDIVIDUAL, &val_v, &err_v);
}
cout << float( clock () - begin_time_v ) / CLOCKS_PER_SEC<<endl;
printf("Computed integral = %0.10g +/- %g\n", val_v, err_v);
cin.get();
}
这里有一些额外的定义:
/* a vector integrand of a vector of npt points: x[i*ndim + j] is the
j-th coordinate of the i-th point, and the k-th function evaluation
for the i-th point is returned in fval[i*fdim + k]. Return 0 on success
or nonzero to terminate the integration. */
typedef int (*integrand_v) (unsigned ndim, size_t npt,
const double *x, void *,
unsigned fdim, double *fval);
/* as hcubature, but vectorized integrand */
int hcubature_v(unsigned fdim, integrand_v f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
size_t maxEval, double reqAbsError, double reqRelError,
error_norm norm,
double *val, double *err);
您应该使用用于定义 integrand_v
的确切类型。
而不是:
int f_v(unsigned ndim, unsigned npts, const double *x, void *fdata, unsigned fdim, double *fval) {
^^^^^^
使用
int f_v(unsigned ndim, size_t npt, const double *x, void *fdata, unsigned fdim, double *fval) {
^^^^^^
它在 32 位中工作可能是因为 size_t
和 unsigned
碰巧是同一类型。
它不适用于 64 位,因为它们是不同的类型。
我有一个Visual Studio2012年用win 32平台编译运行的程序,换平台到X64后报错如下:
" test_integration.cpp(27): error C2664: 'hcubature_v' : 无法将参数 2 从 'int (__cdecl *)(unsigned int,unsigned int,const double *,void *,unsigned int,double *)' 转换为 'integrand_v' "
有人可以提供一些建议吗?谢谢!
#include <iostream>
#include "cubature.h"
#include <ctime>
#include <limits>
using namespace std;
int f_v(unsigned ndim, unsigned npts, const double *x, void *fdata, unsigned fdim, double *fval) {
double sigma = *((double *) fdata);
unsigned i, j;
for (j = 0; j < npts; ++j) { // evaluate the integrand for npts points
double sum = 0;
for (i = 0; i < ndim; ++i) sum += x[j*ndim+i] * x[j*ndim+i];
fval[j] = exp(-sigma * sum);
};
return 0; // success
}
int main(){
double xmin[3] = {-2,-2,-2}, xmax[3] = {2,2,2}, sigma = 0.5;
double val_v,err_v, time_v;
const clock_t begin_time_v = clock();
for (int j = 0; j<1000;j++)
{
// hcubature_v calculates a multidimensional integration.
hcubature_v(1, f_v, &sigma, 1, xmin, xmax, 0, 0, 1e-4, ERROR_INDIVIDUAL, &val_v, &err_v);
}
cout << float( clock () - begin_time_v ) / CLOCKS_PER_SEC<<endl;
printf("Computed integral = %0.10g +/- %g\n", val_v, err_v);
cin.get();
}
这里有一些额外的定义:
/* a vector integrand of a vector of npt points: x[i*ndim + j] is the
j-th coordinate of the i-th point, and the k-th function evaluation
for the i-th point is returned in fval[i*fdim + k]. Return 0 on success
or nonzero to terminate the integration. */
typedef int (*integrand_v) (unsigned ndim, size_t npt,
const double *x, void *,
unsigned fdim, double *fval);
/* as hcubature, but vectorized integrand */
int hcubature_v(unsigned fdim, integrand_v f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
size_t maxEval, double reqAbsError, double reqRelError,
error_norm norm,
double *val, double *err);
您应该使用用于定义 integrand_v
的确切类型。
而不是:
int f_v(unsigned ndim, unsigned npts, const double *x, void *fdata, unsigned fdim, double *fval) {
^^^^^^
使用
int f_v(unsigned ndim, size_t npt, const double *x, void *fdata, unsigned fdim, double *fval) {
^^^^^^
它在 32 位中工作可能是因为 size_t
和 unsigned
碰巧是同一类型。
它不适用于 64 位,因为它们是不同的类型。