gpt4 book ai didi

c++ - 为什么这段代码可以在 Visual Studio 2003 中编译?

转载 作者:行者123 更新时间:2023-11-27 22:35:15 24 4
gpt4 key购买 nike

这可能是个菜鸟问题。

在将项目从 Visual Studio 2003 迁移到 2017 时,我注意到代码会在第 85 行出现“未定义标识符”错误。这对我来说很有意义,因为 nCurrPel 似乎不存在于该范围内。

但是,完全相同的代码似乎在 Visual Studio 2003 中编译得很好。这是怎么回事?

完整代码如下,引用注释部分:

LTBOOL CompressLMData(const int *pData, int width, int height, int *pOutBuffer, int &outLen)
{
if((pData == NULL) || (pOutBuffer == NULL))
{
ASSERT(FALSE);
return FALSE;
}

if((width > LIGHTMAP_MAX_PIXELS_I) || (height > LIGHTMAP_MAX_PIXELS_I))
{
ASSERT(FALSE);
return FALSE;
}

int nBufferLen = width * height * 3;
int nSpanLen = 0;
int* pOutPos = pOutBuffer;

for(int nCurrPel = 0; nCurrPel < nBufferLen; nCurrPel += 3)
{
int nRunLen = 1;
for(int nRunPel = nCurrPel + 3; nRunPel < nBufferLen; nRunPel += 3, nRunLen++)
{
if(nRunLen > 127)
{
break;
}

if( (pData[nCurrPel + 0] != pData[nRunPel + 0]) ||
(pData[nCurrPel + 1] != pData[nRunPel + 1]) ||
(pData[nCurrPel + 2] != pData[nRunPel + 2]) )
{
break;
}
}

if(nRunLen >= 2)
{
if(nSpanLen > 0)
{
OutputSpan(false, nSpanLen, &pData[nCurrPel - nSpanLen * 3], pOutPos);
}

OutputSpan(true, nRunLen, &pData[nCurrPel], pOutPos);
nSpanLen = 0;
nCurrPel += (nRunLen - 1) * 3;
}
else
{
nSpanLen++;
if(nSpanLen > 127)
{
OutputSpan(false, nSpanLen, &pData[nCurrPel - (nSpanLen - 1) * 3], pOutPos);
nSpanLen = 0;
}
}
}

//How does this work? nCurrPel is not defined
if(nSpanLen > 0)
{
OutputSpan(false, nSpanLen, &pData[nCurrPel - nSpanLen * 3], pOutPos);
}

outLen = (int)(pOutPos - pOutBuffer);
ASSERT( (outLen >= 4 * (width * height / 128)) &&
(outLen <= (width * height * 3 + (width * height + 127) / 128)) );

return LTTRUE;
}

最佳答案

这与for 循环的范围有关。

版本 8(2005、2003、2002、6.0 等)之前的 Microsoft Visual C++ 编译器将 for 语句内声明的变量范围“提升”到父范围。在 Visual Studio 2008(版本 9)之后,编译器符合 C++ 规范,该规范要求这些变量的范围仅限于 for 语句及其主体。

Visual C++ 2003 和 2005 接受的不兼容的 C++:

cout << bar << endl; // error: `bar` isn't defined yet

for( int bar = 0; bar < 10; bar++ ) // `bar` is defined here and it can be used within the `for( ... )` statement parenthesis.
{
cout << bar << endl; // this
}

cout << bar << endl; // the life of `bar` extends after its `for` statement to the end of the parent scope

自 Visual C++ 2008 起,bar 的生命周期仅限于 for 语句括号和语句主体, 仅限于父范围:

cout << bar << endl; // error: `bar` isn't in-scope yet

for( int bar = 0; bar < 10; bar++ ) // `bar` is defined here and it can be used within the `for( ... )` statement parenthesis.
{
cout << bar << endl; // OK
}

cout << bar << endl; // error: `bar` is not in scope

要解决此问题,您可以在父作用域中手动声明和初始化 for 循环变量,或者使用单独的变量来保存最后一个值,如下所示:

方法 1:声明移至父范围:

int bar = 0;
for( ; bar < 10; bar++ )
{
cout << bar << endl;
}

cout << bar << endl;

方法 2:在父作用域中声明单独的变量:

int last_bar = 0; // Set an initial value in case the for loop body is never evaluated.
for( int bar = 0; bar < 10; bar++ )
{
cout << bar << endl;

last_bar = bar;
}

cout << last_bar << endl;

关于c++ - 为什么这段代码可以在 Visual Studio 2003 中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55349168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com