警告:语句无效(C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7951662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:47:02  来源:igfitidea点击:

warning: statement has no effect (C++)

c++

提问by Homunculus Reticulli

I have the following code:

我有以下代码:

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);
                }   
            }   
        }   
    }   
}

When compiling, (g++), the warning (statement has no effect) is issued for line 92 & 95 (indicated in the snippet above)

编译 (g++) 时,第 92 行和第 95 行发出警告(语句无效)(在上面的代码段中指示)

I can't see why they have no effect even though I have been staring at it for a while - could do with a second pair of eyes to see if they can spot what I'm missing.

我不明白为什么它们没有效果,即使我已经盯着它看了一会儿——可以用第二双眼睛来看看它们是否能发现我遗漏的东西。

回答by Lekensteyn

It's about the coland rowpart in the initializer part of the loops. Those statements do nothing. Just remove it:

它是关于循环初始化部分中的colrow部分。这些陈述没有任何作用。只需删除它:

for( ; col < num_cols; col++)

回答by b.buchhold

If guess you want to iterate over all columns and for each of them over all rows. so better change your code to something like this:

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

The first statement in the for loop is executed once, i.e. at the time the loop is entered at first. Since you want to include row number zero for all further columns you have to set row to 0 for each column:

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);
                }   
            }   
        }   
    }   
}

回答by FailedDev

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

col;

This has no effect. You have to assign something to it, or don't write it at all. Since you assign it out of your loop you just need to leave an empty;at that place or use while loops.

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

回答by bwDraco

You're getting these warnings because the initialization statements in the forloops are expressions that does nothing:

您收到这些警告是因为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

Since you've already initialized these variables outside the loop, you might want to omit them from the forstatement:

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

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

However, the best thing to do here is to initialize the variables in the forloops themselves rather than outside them:

但是,这里最好的做法是在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);
        }   
    }   
}

回答by AlQafir

If you really want to type the variable names in your for cycle (as a tip maybe?), try by changing:

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

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

to something like this:

像这样:

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

for both the lines. It should do the job.

对于两条线。它应该完成这项工作。

回答by Vladimir

My guess is you could simply use

我的猜测是你可以简单地使用

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

and

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