views:

4522

answers:

5

I'm getting an error in Visual C++ that is giving me a really hard time.

The error is error c2143 reading: syntax error: missing ')' before 'constant'

My code line is:

coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) 2 * depth);

I have #include at the beginning of the file which should define the floor(double) function.

a bit more explanation of the variables.

double depth is a member variable of the class which this line can be found in.
int i is an incrementing index value.
double t is an incrementing value.

What they do is really unimportant, but I wanted to clarify that all three are already defined as variables of basic types.

I've gone through and verified that all the parentheses match up. I'm kind of at a loss as to what 'constant' the compiler is referring to. Any ideas?

+5  A: 
coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) (the problem is here) 2 * depth);
Maurice Perry
+5  A: 

I'm not quite sure if this is the same error that the compiler is giving you, but you have to put a '*' sign in front of the second '2' so that this:

coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) 2 * depth);

Becomes this:

coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) * 2 * depth);
DeadHead
Ah, silly me. Can be hard to see simple mistakes without a second pair of eyes. Thanks.
Rich
+1  A: 

coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) 2(What is 2 doing here?) * depth);

Shree
+3  A: 

Other posters have shown you the actual error in the statement, but please, split that up into multiple sub-statements that more clearly show what you are trying to do mathematically, because that function is going to cause you headaches in the future if you don't!

MadKeithV
alternatively they could just simplify the expression, assuming the missing operator is supposed to be a two then this is a good start..1 + (t - floor( t + 0.5 )) * 2 * depth
tolomea
That (t-floor(t+0.5)) looks like a sub-expression that needs some explanation to me.
MadKeithV
A: 

Even though you have the right answer, I'm going to explain how you should have arrived at it.

When faced with an error in a long expression that you can't find, take the expression apart, piece by piece, until you find it.

In this case:

coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) 2 * depth);

becomes:

firsthalf = (1 - (2 * depth));
secondhalf = ((t - floor( t + 0.5 ) + 1 ) 2 * depth);   // Error appears on this line
coefficient[i] = firsthalf + secondhalf;

This eliminates the first part as the source of the error.

Next attempt:

exprA = (t - floor( t + 0.5 ) + 1 );
exprB = exprA * 2;
exprC = exprB * depth;   // Hmm.... this all worked.  Start putting it back together.
secondhalf = exprC;

Final attempt:

exprA = (( MY_TEST_CONSTANT ) 2 * depth);   // Error now becomes obvious.
abelenky