gpt4 book ai didi

c++ - Goto before variable initialization 导致编译错误

转载 作者:可可西里 更新时间:2023-11-01 18:09:41 33 4
gpt4 key购买 nike

考虑这段代码(VS2008):

void WordManager::formatWords(std::string const& document)
{
document_ = document;
unsigned int currentLineNo = 1;

size_t oldEndOfLine = 0;
size_t endOfLine = document_.find('\n');
while(endOfLine != std::string::npos)
{
std::string line = document_.substr(oldEndOfLine, (endOfLine - oldEndOfLine));
if(line.size() < 2)
{
oldEndOfLine = endOfLine + 1;
endOfLine = document_.find('\n', oldEndOfLine);
continue;
}

std::vector<std::string> words = Utility::split(line);
for(unsigned int i(0); i < words.size(); ++i)
{
if(words[i].size() < 2)
continue;
Utility::trim(words[i], WordManager::delims);
Utility::normalize(words[i], WordManager::replace, WordManager::replaceWith);

if(ruleOne(words[i]) && ruleTwo(words[i]))
{
std::set<Word>::iterator sWIter(words_.find(Word(words[i])));

if(sWIter == words_.end())
words_.insert(Word(words[i])).first->addLineNo(currentLineNo);
else
sWIter->addLineNo(currentLineNo);
}
}
++currentLineNo;

oldEndOfLine = endOfLine + 1;
endOfLine = document_.find('\n', oldEndOfLine);
}
}

如果它很重要:这是家庭作业中的代码,用于过滤和修改文档中的单词。 document 保存文档(之前从文件中读取)

我想引入一个恶意的 goto,因为我认为在这种情况下它实际上更干净:

void WordManager::formatWords(std::string const& document)
{
document_ = document;
unsigned int currentLineNo = 1;

size_t oldEndOfLine = 0;
size_t endOfLine = document_.find('\n');
while(endOfLine != std::string::npos)
{
std::string line = document_.substr(oldEndOfLine, (endOfLine - oldEndOfLine));
// HERE!!!!!!
if(line.size() < 2)
goto SkipAndRestart;

std::vector<std::string> words = Utility::split(line);
for(unsigned int i(0); i < words.size(); ++i)
{
if(words[i].size() < 2)
continue;
Utility::trim(words[i], WordManager::delims);
Utility::normalize(words[i], WordManager::replace, WordManager::replaceWith);

if(ruleOne(words[i]) && ruleTwo(words[i]))
{
std::set<Word>::iterator sWIter(words_.find(Word(words[i])));

if(sWIter == words_.end())
words_.insert(Word(words[i])).first->addLineNo(currentLineNo);
else
sWIter->addLineNo(currentLineNo);
}
}

SkipAndRestart:
++currentLineNo;

oldEndOfLine = endOfLine + 1;
endOfLine = document_.find('\n', oldEndOfLine);
}
}

此时此刻,这是否是一个好的设计选择是无关紧要的。编译器报错 error C2362: initialization of 'words' is skipped by 'goto SkipAndRestart'

我不明白这个错误。为什么跳过单词初始化很重要,而且会出错?这正是我想要发生的,我不想让它做更多的工作,只是重新启动该死的循环。 continue 宏不会做或多或少完全相同的事情吗?

最佳答案

您正在跳过 words 数组的构造:

    if(line.size() < 2)
goto SkipAndRestart;
std::vector<std::string> words = Utility::split(line);
// ...
SkipAndRestart:

可能SkipAndRestart: 标签之后使用了 words,这将是一个问题。你不在你的情况下使用它,但是 words 变量直到它被引入的范围的 end 才会被破坏,所以就对于编译器而言,该变量仍在标签处使用。

你可以通过将 words 放在它自己的范围内来避免这种情况:

    if(line.size() < 2)
goto SkipAndRestart;
{
std::vector<std::string> words = Utility::split(line);
// ...
}
SkipAndRestart:

请注意,continue 语句跳转到循环的末尾,在您实际上不能放置标签的位置。这是在循环内的任何局部变量被破坏之后,但在跳回到循环顶部之前的一个点。

关于c++ - Goto before variable initialization 导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777266/

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