2013-01-11 15 views
0

当我尝试使用头文件添加函数RK4时,我尝试运行以下代码时出现以下错误消息。将函数传递给函数并使用头文件时出错

C:\文件\ C代码\ RK4 \ addRK4.h | 7 |错误:预期 ')' 前 '(' 令牌|

还有一堆后,其他的错误消息,但我不我认为它们很重要,我无法弄清楚什么是错误的,特别是当我在main.cpp中定义RK4的原型时,所有的东西都运行得很好,相关的代码如下:任何关于这个问题的帮助(或者如果你有任何其他建议,因为我很新的C++),将不胜感激!

的main.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); 
} 

} 

addRK4.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; 

} 

回答

2

你有一个符号冲突。

#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 
+0

谢谢,太好了!我从来没有想过这件事。 – user1968603

+0

没问题。它困扰了我一段时间,但是当您将原型放入主源文件而不是使用标题时,您提供了一个关于它的工作正常的关键提示。快乐编码=) – paddy

1

这似乎是有点问题 types Vector2dVectorXd

编辑:good catch @Paddy。由于它仍然有效,所以将其余的答案留在这里。

此外,您声明addRK4.h犯规在RK4.cpp定义匹配。这将是您将要解决的下一个错误