【问题标题】:Expected a Declaration期待声明
【发布时间】:2013-05-02 10:58:50
【问题描述】:

这是我的第一个问题(对编码来说非常新),所以会尝试尽可能多地包含信息,因为现在我很难过!

我正在尝试编写代码来创建一个适合用户输入规范的巴特沃斯过滤器。我正在使用 Code Composer 4。

google 了 54 个错误,我还剩下 1 个:

"expected a declaration" on line 27: if (n=1 && hpf=0)

我有三次检查的大括号,有什么想法吗?

编辑:

您好,再次感谢您的帮助。从那以后解决了旧问题等等,但又碰壁了;代码不会覆盖(或在文件被删除时创建)coffic.cof 文件。没有出现错误,但文件保持不变。有什么想法吗?

附:对之前的代码布局感到抱歉 - 希望这会更好:

#include "dsk6713_aic23.h"      //codec-DSK support file
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ;  //set sampling rate
# include <stdio.h>
# include <math.h>
# include <stdlib.h> 
#define pi 3.1415927
#include "coffic.cof"

void main()
{
    double hpf, fs, fco, atn, fat, tp, k, ad, m, n, o, da, db, dc;
    FILE *fp;
    int c, d, e, f, g, h, i, q, r, s, t, u, v;
    hpf = 0;            //for a high-pass filter input 1, for a low-pass filter input 0
    fs = 8000;          //input samping frequency here
    fco = 2400;         //input cut-off frequency here
    atn = 17;           //input attenuation (dB) here
    fat = 3500;         //input the frequency of attenuation here
    tp = 1/fs;
    k = tan(pi*fco*tp);
    ad = tan(pi*fat*tp);
    m = (log10((pow(10,(atn/10)))-1))/(2*(log10(ad/k)));
    o = abs(m);
    n = ceil(o);

    da = 1.414;
    c = (pow(2,15)*k*k/(1+da*k+k*k));
    d = (pow(2,15)*2*k*k/(1+da*k+k*k));
    e = (pow(2,15)*k*k/(1+da*k+k*k));
    q = (pow(2,15)*(2-2*k*k)/(1+da*k+k*k));
    r = (pow(2,15)*(-1+k-k*k)/(1+da*k+k*k));

    fp = fopen("U:\DSK6713\Ivo\CSP\coffic.cof", "w");
    if (fp == NULL)
    {
        printf("Error. Unable to open coffic.cof");
        exit(0);
    }

    fprintf(fp, "int a[3]={%d, d%, %d};\n", c, d, e);
    fprintf(fp, "int b[3]={1, d%, %d};\n", q ,r);
    fprintf(fp, "int x[3]={0,0,0};\nint y[3]={0,0,0};\n");

    fflush(fp);

    fclose(fp);

    comm_intr();                   //init DSK, codec, McBSP
    while(1);                      //infinite loop
}

interrupt void c_int11()         //interrupt service routine 
{
    short input;
    FILE *fp;
    fp = fopen("U:\DSK6713\Ivo\CSP\coffic.cof", "r");
    if (fp == NULL)
    {
        printf("Error. Unable to open coffic.cof");
        exit(0);
    }

    fclose(fp);

    x[2]=x[1];
    x[1]=x[0];
    y[2]=y[1];
    y[1]=y[0];

    input=input_sample();
    x[0]=input;

    y[0]=a[0]*x[0]+a[1]*x[1]+a[2]*x[2]+b[1]*y[1]+b[2]*x[2];     

    y[0]=y[0]>>15;                      
    input=(short)y[0];
    output_sample(input);   //output data  
    return;
}

【问题讨论】:

    标签: c if-statement declaration code-composer


    【解决方案1】:

    n=ceil(o) 之后的大括号在做什么?

    您正在结束主函数,这会使您的 if 语句超出函数范围。

    删除那个大括号,代码应该可以工作了。

    此外,在 if 语句中使用 == 而不是 =。 = 会给你的变量赋值,顺便说一下,它们总是返回 true,== 实际上比较它们。

    【讨论】:

      【解决方案2】:

      此外,您的程序似乎缺少 main() 函数,在顶层(任何函数之外)有代码,这是不允许的。

      主程序应该在一个名为的函数中:

      int main(void)
      

      因为这是开始执行的地方。

      此外,C 中的比较运算符拼写为 ==。单个= 是赋值。

      【讨论】:

        【解决方案3】:

        您正试图在 main 之外执行一些代码并且未声明为函数。您需要将所有代码放在 main.js 中。在第 27 行,您关闭了 main 并且没有将下一个语句声明为函数。所以

        #include "dsk6713_aic23.h"      //codec-DSK support file
        Uint32 fs=DSK6713_AIC23_FREQ_8KHZ;  //set sampling rate
        # include <stdio.h>
        # include <math.h>
        # include <stdlib.h> 
        #define pi 3.1415927
        
        int main(int argc, char *argv[])
        {
         double hpf, fs, fco, atn, fat, tp, k, ad, m, n, o, da, db, dc;
         int c, d, e, f, g, h, i, q, r, s, t, u, v;
         hpf = 0;            //for a high-pass filter input 1, for a low-pass filter input 0
         fs = 8000;          //input samping frequency here
         fco = 2400;         //input cut-off frequency here
         atn = 17;           //input attenuation (dB) here
         fat = 3500;         //input the frequency of attenuation here
         tp = 1/fs;
         k = tan(pi*fco*tp);
         ad = tan(pi*fat*tp);
         m = (log10((pow(10,(atn/10)))-1))/(2*(log10(ad/k)));
         o = abs(m);
         n = ceil(o);
        
        
         if (n == 1 && hpf == 0)
         {
          int     a[2]={c,d};
          int     b[2]={1,q};
          int     x[2]={0,0};
          int     y[2]={0,0};
          c = (pow(2,15)*k/(k+1));
          d = (pow(2,15)*k/(k+1));
          q = (pow(2,15)*(1-k)/(k+1));
         }
        
         else if (n == 2 && hpf == 0)
         {
          da = 1.414;
          int     a[3]={c,d,e};
          int     b[3]={1,q,r};
          int     x[3]={0,0,0};
          int     y[3]={0,0,0};
          c = (pow(2,15)*k*k/(1+da*k+k*k));
          d = (pow(2,15)*2*k*k/(1+da*k+k*k));
          e = (pow(2,15)*k*k/(1+da*k+k*k));
          q = (pow(2,15)*(2-2*k*k)/(1+da*k+k*k));
          r = (pow(2,15)*(-1+k-k*k)/(1+da*k+k*k));
         }
        
         else
         {
          puts("Sorry, the parameters you have entered cannot be met by a Butterworth Filter of order 6 or less.");
         }
        
          comm_intr();                   //init DSK, codec, McBSP
          while(1);                      //infinite loop
        
        } // closing brace of main
        
        interrupt void c_int11()         //interrupt service routine 
        {
         short input;
        
         x[1]=x[0];
         y[1]=y[0];
        
         input=input_sample();
         x[0]=input;
        
         y[0]=a[0]*x[0]+a[1]*x[1]+b[1]*y[1];     
        
         y[0]=y[0]>>15;                      
         input=(short)y[0];
         output_sample(input);   //output data  
         return;
        }
        

        你不是在比较,而是在分配。改为

        if (n == 1 && hpf == 0){ }
        

        同样少hint

        如果你执行

        if( n = 1 )
        

        这是一个有效的表达式,但它是一个赋值,在这种特殊情况下将返回true,但它总是返回true。 而

        if ( n == 1)
        

        是一个比较,如果 n 等于 1,则返回 true,如果 n 不等于 1,则返回 false。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-18
          • 1970-01-01
          • 2021-05-26
          • 2021-02-09
          • 2018-06-12
          • 2017-01-26
          相关资源
          最近更新 更多