Weird error for a basic equation

I have the weirdest error ever, it claims that what I've wrote isn't an expression when it clearly is. Unless I'm making a stupid mistake and can't see it, any help would be greatly appreciated.

here is the code(please put aside the fact that some variables aren't referenced and stuff, they are in the original code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <cutil.h>
#include <cutil_inline.h>
#include <fstream>
#include <cstdlib>
#include <complex>

#define sec 1/cos
#define s_x ((1/k_x)*sin(k_x*L))
#define d_x 6*(1 - cos(k_x*L)/(k_x*k_x))
#define c_x cos(k_x*L)
#define J_1 ((L - s_x)/(k_x*k_x*))
#define J_2 (3*L - 4*s_x + s_x*c_x)/(2*k_x*k_x*k_x*k_x)
#define J_d 6

using namespace std;

int main(){


struct transport_map2{
	double drift[6][6][6];
	struct bend{
		double entrance[6][6][6];
		double exit[6][6][6];
		double dipole[6][6][6];
	} rbend, sbend;
	double quadrupole[6][6][6];
	double sextupole[6][6][6];
} T;

	double Beta_s = 0.9797958971; //Assign a value to relativistic Beta (Obviously t6is will be determined by program at a later stage)
	double L = 1000; //Lengt6 of t6e drift space
	double K_1 = 5; //(for sbend and rbend): Quadrupole Field Strengt6 in combined - function dipole. (for quadrupole): Normal Quadrupole field strengt6 (coefficient). Positive K_1 means 6orizontally focussing.
	double K_2 = 5; //Normal sextupole field strengt6

	double k_x = 1.0;

	T.sbend.dipole[0][0][5] = -((6 / (12 * Beta_s)) * (K_2 + 2 * 6 * K_1) * (3 * s_x * J_1 - d_x * d_x) + ((6 * 6) / (2 * Beta_s)) * (s_x * s_x) + (1 / (4 * Beta_s)) * K_1 * L * s_x);



}


The error says:

Error 1 error: expected an expression

with regards to the last line of the code (with T.sbend.dipole[0][0][5])

Many Thanks,
Phill
PHamnett wrote:
...it claims that what I've wrote isn't an expression when it clearly is.


#define J_1 ((L - s_x)/(k_x*k_x*)) What is that final * supposed to do?

Should that be:

#define J_1 ((L - s_x)/(k_x*k_x)) ?


...Compiler 1 PHamnett 0.
Galik is correct; your definition of J_1 will definitely cause a problem. I'm curious - how did you try debugging this initially? Looking at the last line listed above alone, it would be very very difficult to spot the error, especially when you have macro expansion to consider. One of the most important skills I've gained in the workplace is the ability to effectively debug problems (without the help of the cplusplus.com community :)). In this case, you could try either:
1) unit testing all your macros
and/or
2) breaking out the expression into a series of smaller expressions, allowing the compiler to show you exactly where your problem is.

so:
1
2
3
4
5
   int v1 = (6 / (12 * Beta_s));
   int v2 = (K_2 + 2 * 6 * K_1);
   int v3 = (3 * s_x * J_1 - d_x * d_x);
   ...
   T.sbend.dipole[0][0][5] = -(v1 * v2 *...)


This would show you the error is in the initialization of v3, which you could then examine more closely.

Also as an aside - I don't know how others feel about this, but IMO having macros that rely on the existence of local variables of a particular name seems a bit sloppy...

--Rollie
rollie++
Thankyou everyone, and Rollie I will try to debug better next time, I'm still fairly new to programming.
Many thanks again!
Phill
Topic archived. No new replies allowed.