Visual C ++错误C2143:语法错误:在'常量'之前缺少')'

时间:2009-04-06 07:08:57

标签: c++ visual-studio syntax-error

我在Visual C ++中遇到错误,这让我很难过。

错误是错误c2143读取:语法错误:在'常数'之前缺少')'

我的代码行是:

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

我在文件的开头有#include,它应该定义floor(double)函数。

对变量的更多解释。

double depth是该行的成员变量,该行可以在其中找到 int i是递增的索引值 double t是递增值。

他们所做的事实上并不重要,但我想澄清一下,这三者都已被定义为基本类型的变量。

我已经完成并验证所有括号都匹配。关于编译器所指的“常量”,我有点不知所措。有什么想法吗?

6 个答案:

答案 0 :(得分:6)

我不太确定这是否与编译器给你的错误相同,但是你必须在第二个'2'前加上一个'*'符号,这样就可以了:

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

成为这个:

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

答案 1 :(得分:6)

其他海报已经向您展示了声明中的实际错误,但请将其拆分为多个子语句,以便更清楚地显示您尝试以数学方式执行的操作,因为该功能将在未来引起您的头痛如果你不这样做!

答案 2 :(得分:5)

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

答案 3 :(得分:2)

即使你有正确的答案,我也会解释你应该如何应对它。

当遇到无法找到的长表达式中的错误时,请将表达式逐个分开,直到找到它为止。

在这种情况下:

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

变为:

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

这消除了第一部分作为错误的来源。

下一次尝试:

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

最后的尝试:

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

答案 4 :(得分:1)

系数[i] =(1 - (2 *深度))+((t - floor(t + 0.5)+ 1) 2(这里有2个做什么?) *深度) ;

答案 5 :(得分:1)

在声明枚举时我遇到了类似的错误。这是因为其中一个枚举常量也在代码中的其他地方声明。