C++ 用C+重构代码+;
类路由的构造函数最初包含以下代码,用于检查文件中是否存在元素(“gpx”、“rte”等)。它运行正常C++ 用C+重构代码+;,c++,refactoring,gpx,C++,Refactoring,Gpx,类路由的构造函数最初包含以下代码,用于检查文件中是否存在元素(“gpx”、“rte”等)。它运行正常 if (! elementExists(source,"gpx")) { oss << endl << "no gpx tag"; constructorReport = oss.str(); constructorSucceeded = false; return; } if (! elementExist
if (! elementExists(source,"gpx"))
{
oss << endl << "no gpx tag";
constructorReport = oss.str();
constructorSucceeded = false;
return;
}
if (! elementExists(source,"rte"))
{
oss << endl << "no rte tag";
constructorReport = oss.str();
constructorSucceeded = false;
return;
}
如果(!element存在(源代码,“gpx”))
{
oss在您的原始代码中,当其中一个测试第一次失败时,您将从函数返回,而不会继续尝试其他测试
现在,您已经将测试移动到了一个函数中,调用方无法知道测试是否失败,因此它将执行所有测试,并且当其中一个测试失败时,不会从其函数返回。您需要此函数返回一个布尔值,指示测试是否失败
bool Route::constCheck(string source, string type)
{
if (! XML_Parser::elementExists(source, type))
{
std::ostringstream oss;
oss << std::endl << "no" << type <<" tag";
constructorReport = oss.str();
constructorSucceeded = false;
return false;
}
return true;
}
你认为return
有什么作用?没有必要为无效函数返回:)但是这里的问题在别处。你能告诉我们你是如何以及在哪里执行你的新函数的吗?在你修改过的函数中,你没有打印到oss
同样的东西:“no”后面没有空格(除非类型
以空格开头,我对此表示怀疑)。不知道这是否是问题所在,这实际上取决于您对它所做的操作,但可能是。我已尝试返回对x。然后设置constructorReport=x.first和constructorSucceeded=x.second的值。我了解我的错误所在。代码运行得非常好,调用它时我只需省略!即可。谢谢帮助!。
bool Route::constCheck(string source, string type)
{
if (! XML_Parser::elementExists(source, type))
{
std::ostringstream oss;
oss << std::endl << "no" << type <<" tag";
constructorReport = oss.str();
constructorSucceeded = false;
return false;
}
return true;
}
if (!constCheck(source, "gpx")) {
return;
}
if (!constCheck(source, "rte")) {
return;
}