gpt4 book ai didi

c++ - 将函数传递给函数并使用头文件时出错

转载 作者:行者123 更新时间:2023-11-28 00:50:36 25 4
gpt4 key购买 nike

当我尝试使用头文件添加函数 RK4 时尝试运行以下代码时,我收到以下错误消息。

C:\Documents\C code\RK4\addRK4.h|7|error: expected ')' before '(' token|

在那之后还有一堆其他的错误信息,但我认为它们并不重要。我不知道出了什么问题,尤其是当我在 main.cpp 中定义 RK4 的原型(prototype)时,一切都运行得很好。相关代码如下。对此事的任何帮助(或者如果您有任何其他建议,因为我是 c++ 的新手)将不胜感激!

主要.cpp

#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include "gnuplot.h"
#include "addfitzhough.h"
#include "addRK4.h"

using namespace std;
using namespace Eigen;

int main()
{

//int mydims = 2;

double u = 0;
double *Iion;
double h = .5;

double y1ans[800];
double y2ans[800];
double tans[800];


Vector2d ycurr;

Vector2d Ynot, yplus;

Ynot << .2,
.1;

y1ans[0] = Ynot(0);
y2ans[0] = Ynot(1);
tans[0] = 0.0;

for(int i = 1;i<800;i++){
tans[i] = tans[i-1] + h;
ycurr << y1ans[i-1],
y2ans[i-1];
yplus = RK4(fitzhough,tans[i],ycurr,h,u,Iion,2);
y1ans[i] = yplus(0);
y2ans[i] = yplus(1);
}

}

添加RK4.h

#ifndef RK4
#define RK4

using namespace Eigen;

VectorXd RK4(VectorXd (*f) (double t, Vector2d Y, double u, double * Iion), double t, VectorXd z, double h, double u, double *Iion, int d);


#endif // RK4

RK4.cpp

#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

Vector2d RK4(Vector2d (*f)(double, Vector2d, double, double*), double t, VectorXd z, double h, double u, double *Iion, int d){

VectorXd Y1(d), Y2(d), Y3(d), Y4(d), Y1buf(d), Y2buf(d), Y3buf(d);

Y1 = z;
Y1buf = (*f)(t,Y1,u, Iion);
Y2 = z + 0.5*h*Y1buf;
Y2buf = (*f)(t+.5*h,Y2,u, Iion);
Y3 = z + 0.5*h*Y2buf;
Y3buf = (*f)(t+.5*h,Y3,u, Iion);
Y4 = z + h*Y3buf;


Vector2d yn = z + (h/6.0)*(Y1buf + 2.0*Y2buf + 2.0*Y3buf + (*f)(t+h,Y4,u, Iion));

return yn;
}

fitzhough.cpp

#include <Eigen/Dense>

using namespace std;
using namespace Eigen;
Vector2d fitzhough(double t, Vector2d Y, double u, double * Iion){

Vector2d dy;

double v = Y(0);
double w = Y(1);

double a = .13;
double b = .013;
double c1 = .26;
double c2 = .1;
double d = 1.0;

dy(0) = c1*v*(v-a)*(1-v)-c2*w*v + u;
dy(1) = b*(v-d*w);

*Iion = dy(0)-u;

return dy;

}

最佳答案

你有一个符号冲突。

#define 符号RK4 然后您尝试创建一个具有该名称的函数。因为您已将其定义为空宏,所以它将被替换为空。然后编译器将其视为您的函数声明:

VectorXd (VectorXd (*f) (double t, Vector2d Y, double u, double * Iion), double t, VectorXd z, double h, double u, double *Iion, int d);

为您的标题栏添加额外的字符是个好主意。像这样的东西:

#ifndef RK4__H
#define RK4__H

关于c++ - 将函数传递给函数并使用头文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270908/

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