visual-studio Visual C++ 错误 C2143:语法错误:在“常量”之前缺少“)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/720440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:20:45  来源:igfitidea点击:

Visual C++ error C2143: syntax error: missing ')' before 'constant'

c++visual-studiosyntax-error

提问by Rich

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

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

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

错误是错误 c2143 读数:语法错误:在 '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.

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

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.

double depth 是该行所在类的成员变量。
int i 是一个递增的索引值。
double t 是一个递增的值。

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?

我已经检查并验证所有括号都匹配。我对编译器所指的“常量”有点不知所措。有任何想法吗?

回答by DeadHead

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:

我不太确定这是否与编译器给您的错误相同,但是您必须在第二个 '2' 前面放一个 '*' 符号,以便:

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);

回答by Joris Timmermans

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!

其他海报已经向您展示了语句中的实际错误,但是请将其拆分为多个子语句,以更清楚地显示您在数学上尝试做的事情,因为如果您不这样做,该函数将来会让您头疼'不!

回答by Maurice Perry

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

回答by abelenky

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.

回答by Pramod

I faced a similar error when declaring an enum. It was because one of the enum constants was also declared elsewhere in the code.

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

回答by Shree

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

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