user1968603
user1968603

Reputation: 17

Error when passing a function to a function and using a header file

I am getting the following error message when I try to run the following code when I try to add the function RK4 using a header file.

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

There are a bunch of other error messages after that but I don't think they are important. I can't figure out what's wrong, especially since when I define the prototype of RK4 in main.cpp, everything runs just fine. The relevant code is below. Any help on this matter (or if you have any other suggestions since I am pretty new to c++) would be greatly appreciated!

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;

}

Upvotes: 0

Views: 117

Answers (2)

Karthik T
Karthik T

Reputation: 31972

It appears to be something wrong with the types Vector2d and VectorXd.

Edit: good catch @Paddy. Leaving the rest of the answer here since it is still valid.

Also, your declaration in addRK4.h doesnt match the definition in RK4.cpp. This will be the next error you will be fixing.

Upvotes: 1

paddy
paddy

Reputation: 63481

You have a symbol clash.

You #define the symbol RK4 and then you try to create a function with that name. Because you have defined it as an empty macro, it will be replaced with nothing. Then the compiler sees this as your function declaration:

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

It's a good idea to add extra characters for your header cages. Something like:

#ifndef RK4__H
#define RK4__H

Upvotes: 2

Related Questions