【问题标题】:Why this error " pngrutil.c:27:error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token" in the following function?为什么在以下函数中出现此错误“ pngrutil.c:27:error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token”?
【发布时间】:2014-05-23 19:34:59
【问题描述】:
double shrDelta(int iCounterID = 0){     //this is the 27  the line

    double DeltaT;            
    static struct timeval _NewTime;  
    static struct timeval _OldTime[3]; 
    gettimeofday(&_NewTime, NULL);  

    if (iCounterID >= 0 && iCounterID <= 2)
        {        

        DeltaT =  ((double)_NewTime.tv_sec + 1.0e-6 * (double)_NewTime.tv_usec) - ((double)_OldTime[iCounterID].tv_sec + 1.0e-6 * (double)_OldTime[iCounterID].tv_usec);        

        _OldTime[iCounterID].tv_sec  = _NewTime.tv_sec;     
        _OldTime[iCounterID].tv_usec = _NewTime.tv_usec;        
        }   
    else        
        {         

        DeltaT = -9999.0;       
        }     

    return DeltaT;
} 

该函数应该返回最后两次调用之间经过的时间。

【问题讨论】:

  • 不能在 C 函数的参数列表中赋值。
  • 我认为您将 C 与其他一些允许您以这种方式指定默认参数的语言混淆了。
  • 一般来说,解释你遇到的问题然后在正文中提出问题是一种很好的形式。
  • 该函数在 cpp 文件中运行良好,但我只是将整个函数复制到 .c 文件中

标签: c


【解决方案1】:

C 不支持函数参数的默认设置(或函数重载)。

你需要写

double shrDelta(int iCounterID)

而是始终使用明确给出的参数值调用函数。

【讨论】:

    【解决方案2】:

    C 标准不支持默认值。您使用的是 C++ 概念。你可以阅读这个相关的question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      相关资源
      最近更新 更多