【问题标题】:warning: statement has no effect (C++)警告:语句无效 (C++)
【发布时间】:2011-10-31 09:12:09
【问题描述】:

我有以下代码:

void CScriptTable::EnumReferences(asIScriptEngine *engine)
{
    if (m_table)
    {   
        // Call the gc enum callback for each nested table      
        size_t col = 0, row = 0, num_cols = m_table->numCols(), num_rows = m_table->numRows();

        for( col; col < num_cols; col++ )   // Line 92
        {   
            if (m_table->getColType(col) == COL_TABLE) {
                for (row; row < num_rows; row++){  // Line 95
                    Table * tbl = m_table->getTable(row, col);
                    engine->GCEnumCallback(tbl);
                }   
            }   
        }   
    }   
}

编译时,(g++),第92&95行发出警告(声明无效)(上面sn-p中表示)

即使我已经盯着它看了一段时间,我也不明白为什么它们没有效果 - 可以用第二双眼睛看看他们是否能发现我错过的东西。

【问题讨论】:

  • 拆分你的变量声明。那条线太疯狂了。你为什么要在循环之外声明你的循环变量呢?不要那样做。
  • 您的代码中有一个细微的错误。 row 在进入 col 循环之前被设置为 0 一次,之后永远不会重置。因此,在外部 (col) 循环的第一次迭代中,您确实对行进行了迭代,但对于所有其他迭代,您什么也不做。请参阅@b.buchhold 的答案,它解决了您的原始问题和这个错误......并学习在尽可能紧凑的范围内声明您的变量。

标签: c++


【解决方案1】:

如果您想遍历所有列,并为每个列遍历所有行。所以最好把你的代码改成这样:

for 循环中的第一条语句执行一次,即在第一次进入循环时。由于您想为所有其他列包含行号为零,因此您必须将每列的行设置为 0:

void CScriptTable::EnumReferences(asIScriptEngine *engine)
{
    if (m_table)
    {   
        // Call the gc enum callback for each nested table      
        size_t num_cols = m_table->numCols(), num_rows = m_table->numRows();

        for(size_t col = 0; col < num_cols; col++ )   // Line 92
        {   
            if (m_table->getColType(col) == COL_TABLE) {
                for (size_t row = 0; row < num_rows; row++){  // Line 95
                    Table * tbl = m_table->getTable(row, col);
                    engine->GCEnumCallback(tbl);
                }   
            }   
        }   
    }   
}

【讨论】:

    【解决方案2】:

    这是关于循环初始化部分中的colrow 部分。这些声明什么也没做。只需将其删除:

    for( ; col < num_cols; col++)
    

    【讨论】:

      【解决方案3】:
      for( col; col < num_cols; col++ )
      
      col;
      

      这没有效果。你必须给它分配一些东西,或者根本不写它。由于您将其分配到循环之外,因此您只需在该位置留下一个空的; 或使用 while 循环。

      【讨论】:

        【解决方案4】:

        您收到这些警告是因为 for 循环中的初始化语句是不执行任何操作的表达式:

        for(col; col < num_cols; col++)  // line 92: "col" has no effect
        for(row; row < num_rows; row++)  // line 95: "row" has no effect
        

        由于您已经在循环外初始化了这些变量,您可能希望在 for 语句中省略它们:

        for(; col < num_cols; col++)  // line 92
        for(; row < num_rows; row++)  // line 95
        

        然而,这里最好的做法是在 for 循环本身而不是在它们之外初始化变量:

        // Call the gc enum callback for each nested table
        size_t num_cols = m_table->numCols(), num_rows = m_table->numRows();
        
        for(size_t col = 0; col < num_cols; col++ )   // Line 92
        {   
            if (m_table->getColType(col) == COL_TABLE) {
                for (size_t row = 0; row < num_rows; row++){  // Line 95
                    Table * tbl = m_table->getTable(row, col);
                    engine->GCEnumCallback(tbl);
                }   
            }   
        }
        

        【讨论】:

          【解决方案5】:

          如果你真的想在你的 for 循环中输入变量名(也许是提示?),请尝试更改:

          for( col; col < num_cols; col++ )
          

          到这样的事情:

          for( col = col; col < num_cols; col++ )
          

          对于这两行。它应该可以完成这项工作。

          【讨论】:

          • 这有可能隐藏未初始化的变量警告,或生成特别糟糕的“变量自行初始化”警告。我什至见过一个脆弱的编译器崩溃。而且风格很差。
          • 当 'col' 尚未初始化时会发生该错误,但事实并非如此。我同意你的观点,这真的很糟糕,但我假设他知道正确的语法。
          【解决方案6】:

          我猜你可以简单地使用

          for(; col < num_cols; col++ )   // Line 92
          

          for (; row < num_rows; row++) {  // Line 95
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-14
            • 2011-08-09
            • 1970-01-01
            • 1970-01-01
            • 2017-07-16
            • 2018-05-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多