1

我在尝试编译我的单元测试代码时遇到了一个奇怪的错误。出于某种原因,编译器将我的sscanf调用视为混合声明?我不太明白,这是整个错误:

cc1: warnings being treated as errors
/home/brlcad/brlcad/src/libbn/tests/bn_complex.c: In function 'main':
/home/brlcad/brlcad/src/libbn/tests/bn_complex.c:53: error: ISO C90 forbids mixed declarations and code
make[2]: *** [src/libbn/tests/CMakeFiles/tester_bn_complex.dir/bn_complex.c.o] Error 1
make[1]: *** [src/libbn/tests/CMakeFiles/tester_bn_complex.dir/all] Error 2
make: *** [all] Error 2

int
main(int argc, char *argv[])
{
    double expRe1, expIm2, expSqRe1, expSqIm2;
    double actRe1, actIm2, actSqRe1, actSqIm2;
    actRe1 = actIm2 = actSqRe1 = actSqIm2 =
    expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;
    bn_complex_t com1,com2; //a struct that holds two doubles
    if(argc < 5)
        bu_exit(1, "ERROR: Invalid parameters[%s]\n", argv[0]);

    sscanf(argv[1], "%lf,%lf", &com1.re, &com1.im); /* Error is HERE */
    sscanf(argv[2], "%lf,%lf", &com2.re, &com2.im);
    sscanf(argv[3], "%lf,%lf", &expRe1, &expIm2);
    sscanf(argv[4], "%lf,%lf", &expSqRe1, &expSqIm2);

    test_div(com1, com2, &actRe1, &actIm2);
    test_sqrt(com1,com2, &actSqRe1, &actSqIm2);

    if((fabs(actRe1 - expRe1) < 0.00001) || (fabs(actIm2 - expIm2) < 0.00001)){
        printf("Division failed...\n");
        return 1;
    }
    if((fabs(actSqRe1 - expSqRe1) < 0.00001) || (fabs(actSqIm2 - expSqIm2) < 0.00001)){
        printf("Square roots failed...\n");
        return 1;
    }
    return 0;
}
4

3 回答 3

7

在 C90 中,所有声明都必须出现在块的开头。把这个:

bn_complex_t com1,com2; //a struct that holds two doubles

以上这些陈述:

actRe1 = actIm2 = actSqRe1 = actSqIm2 =
expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;
于 2012-12-14T22:48:33.663 回答
3

C90 要求所有声明在块内的所有语句之前。

C99(如 C++)允许混合声明和语句。

你有一个赋值语句:

actRe1 = actIm2 = actSqRe1 = actSqIm2 =
expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;

后跟声明:

bn_complex_t com1,com2;

(它不应该抱怨这些sscanf调用;根据您的源文件检查错误消息中的行号。)

您可以重新排列代码,也可以修改您的代码,Makefile以便在 C99 模式下调用编译器。您似乎在使用 gcc;使用-std=c99(或者-std=gnu99如果您想要特定于 GNU 的扩展。(如何执行此操作的详细信息取决于您Makefile的编写方式。)

或者,您可以用初始化程序(不算作语句)替换分配。

就个人而言,我更喜欢将每个声明放在一行上。我会改变这个:

double expRe1, expIm2, expSqRe1, expSqIm2;
double actRe1, actIm2, actSqRe1, actSqIm2;
actRe1 = actIm2 = actSqRe1 = actSqIm2 =
expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;

对此:

double expRe1   = 0.0;
double expIm2   = 0.0;
double expSqRe1 = 0.0;
double expSqIm2 = 0.0;

double actRe1   = 0.0;
double actIm2   = 0.0;
double actSqRe1 = 0.0;
double actSqIm2 = 0.0;

但是如果你喜欢更紧凑的布局,你可以写:

double expRe1 = 0.0, expIm2 = 0.0, expSqRe1 = 0.0, expSqIm2 = 0.0;
double actRe1 = 0.0, actIm2 = 0.0, actSqRe1 = 0.0, actSqIm2 = 0.0;

即使您在一行上声明了多个变量,您仍然需要为每个变量创建一个初始化程序。

于 2012-12-14T22:59:03.377 回答
2

我不确定为什么编译器会在此处定位错误,但 C90 要求所有变量声明都位于块的顶部,因此bn_complex_ts 有错误。

于 2012-12-14T22:48:15.533 回答