有没有办法访问for循环块之外的变量

时间:2013-11-24 18:45:52

标签: c++ scope

有没有办法访问for循环块之外的变量?

int seedOffset;

// Loop through the asteroids and give them some starting orientation by using the random values 
for(std::vector<Asteroid*>::iterator asteroid = asteroids.begin(); asteroid != asteroids.end(); ++asteroid) 
{
    ( *asteroid )->posZ = -3.0;
    ( *asteroid )->rotX = GenerateRandomNumber( seedOffset, LOW, HIGH );
    ( *asteroid )->rotY = GenerateRandomNumber( seedOffset, LOW, HIGH );
    ( *asteroid )->rotZ = GenerateRandomNumber( seedOffset, LOW, HIGH );
    ( *asteroid )->initialSceneZRotation = GenerateRandomNumber( seedOffset, LOW, HIGH );
    seedOffset += 100;
}

错误:

Error   2   error C4700: uninitialized local variable 'seedOffset' used c:\**\game.cpp  53  1   Game

2 个答案:

答案 0 :(得分:1)

错误消息告诉所有人!

int seedOffset = 0; //Initialize the variable before use

答案 1 :(得分:0)

您需要初始化seedOffset变量以避免错误。

int seedOffset = 0;