代码之家  ›  专栏  ›  技术社区  ›  Rich khmarbaise

“VisualC++错误C2143:语法错误:“丢失”)在“常数”之前

  •  3
  • Rich khmarbaise  · 技术社区  · 17 年前

    我在Visual C++中遇到了一个错误,这给了我一个非常困难的时间。

    我的代码行是:

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

    再解释一下变量。

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

    它们的作用其实并不重要,但我想澄清的是,这三个变量都已经被定义为基本类型的变量。

    我已经检查并验证了所有括号是否匹配。对于编译器所指的“常量”,我有点不知所措。有什么想法吗?

    6 回复  |  直到 17 年前
        1
  •  6
  •   DeadHead    17 年前

    我不太确定这是否与编译器给出的错误相同,但必须在第二个“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);
    
        2
  •  6
  •   Joris Timmermans    17 年前

    其他海报已经向您展示了语句中的实际错误,但请将其拆分为多个子语句,以更清楚地显示您试图在数学上做什么,因为如果您不这样做,该函数将在将来给您带来麻烦!

        3
  •  5
  •   Maurice Perry    17 年前
    coefficient[i] = (1 - (2 * depth)) + ((t - floor( t + 0.5 ) + 1 ) (the problem is here) 2 * depth);
    
        4
  •  2
  •   abelenky    17 年前

    当你在一个长表达式中遇到一个你找不到的错误时,把这个表达式一块一块地拆开,直到你找到它为止。

    在这种情况下:

    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.
    
        5
  •  1
  •   Shree    17 年前

    2(2在这里做什么?) *深度);

        6
  •  1
  •   Pramod    14 年前