代码块会抛出有关for_each的预期主表达式错误

时间:2015-04-07 13:01:25

标签: c++ linux ubuntu codeblocks

我正在使用Irrlicht创建游戏,目前正在制作Linux版本。我将代码移到了代码:Blocks并删除了类似pragma的内容,但是我的for_each循环出现了以下错误:

||=== Build: Debug in IrrlichtScene (compiler: GNU GCC Compiler) ===|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp||In function ‘int main()’:|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|115|error: expected primary-expression before ‘mod’|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|115|error: ‘for_each’ was not declared in this scope|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|159|error: expected primary-expression before ‘m’|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|165|error: expected primary-expression before ‘var’|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|171|error: expected primary-expression before ‘an’|
/home/ubuntu/Desktop/IRRPROJECT/TerrainStarter/terrain.cpp|231|warning: unused variable ‘movy’ [-Wunused-variable]|
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

这就是我的循环:

for_each (ModelLoader mod in models)
{
    mod.Load();
}

据我所知,与visual studio相比,代码可能存在细微差别,但我的Visual Studio版本从未遇到任何问题。预先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以在此处使用基于范围的for循环而不是for_each

for (auto& mod : models)
{
    mod.Load();
}

否则,如果您想坚持使用for_each,那么

std::for_each(models.begin(), models.end(), [](ModelLoader& mod){ mod.Load(); });