“可以在此函数中使用未初始化的函数[-Wyybe-uninitialized]”

时间:2016-11-08 16:21:09

标签: c++

我想问一个让我感到困惑的问题。我正在尝试扫描字符串并将其转换为实数。使用该数字来计算价值。这是我的代码:

def f(val):
   # @params: val is the entire column if axis is 0
   # @returns: css style attributes based on the cell value
   return ['color: red' if x < 0 else 'color: blue' for x in val]

html_style = df.style.apply(f, axis = 0, subset = ['column_name']) # this will apply the css style to td of that column

 # above only returns Styler object so you need to convert it to string to render it
html_style.render() # this does the job

当我编译时,我收到了这条消息:

“警告:'f_s'可以在此函数中未初始化使用[-Wyybe-uninitialized] nblocks =(10 /(nsamps / f_s));”

你有什么理想帮我解决这个问题吗?

非常感谢你。

2 个答案:

答案 0 :(得分:3)

这意味着,如果v[i]既不是“mhz”也不是“ksps”,则永远不会执行将内容分配给f_s的代码,从而使f_s保持未初始化状态。

您可以阻止此警告,例如:

for(unsigned i=1; i < v.size(); i++)
{
    if(v[i] == "mhz"){
        f_0 = atoi(v[i-1].c_str())*1e6;                 
    }
    else if(v[i] == "ksps"){
        f_s = atoi(v[i-1].c_str()) * 1e3;// f_s = 8e6;          
    }
    else
    {
      // v[i] is none of the expected values
       f_s = -1;  
       ... take more action
    }
}

答案 1 :(得分:2)

这里的问题是if(v[i] == "ksps")可能永远不会成立。如果不是,那么f_s永远不会获得设置值。你可以做的是默认初始化f_s一些值。然后你至少知道变量有一些已知的状态。

请注意,如果您尚未初始化f_0,则会遇到相同的问题。