[英]confused by YAML::NodeType::Undefined with yaml-cpp
我有一個測試yaml文件,我試圖使用yaml-cpp解析。
test.yaml
testConfig:
# this points to additional config files to be parsed
includes:
required: "thing1.yaml"
optional: "thing2.yaml"
#some extraneous config information
foo: 42
bar: 394
baz: 8675309
我解析它我得到testConfig.Type()
返回YAML::NodeType::Map
。 這是預期的行為。
然后,我嘗試解析includes以獲取我無法迭代的必需值或可選值,因為includes.Type()
返回YAML::NodeType::Undefined
。 我對yaml和yaml-cpp真的很新,所以任何有助於向我展示我出錯的地方都會受到贊賞。
解析代碼:
{includes and other such nonsense}
.
.
.
YAML::Node configRoot = YAML::LoadFile(path.c_str() );
if( configRoot.IsNull() )
{
SYSTEM_LOG_ERROR("Failed to load the config file: %s.",
path.c_str());
return false;
}
YAML::Node includes = configRoot["includes"];
/* ^^^^^^^^^^^^^^^
* I believe that here lies the issue as includes is undefined and
* therefore I cannot iterate over it.
*/
for( auto it = include.begin(); it != include.end(); ++it )
{
// do some fantastically brilliant CS voodoo!
}
.
.
.
{ more C++ craziness to follow }
解決方案:我刪除了不必要的頂級configTest
以便我可以根據需要解析包含。
那么,您的頂級YAML文檔確實沒有包含名為includes
的鍵。 它只包含一個名為testConfig
的密鑰。 你應該先訪問它:
// ...
YAML::Node configRoot = YAML::LoadFile(path.c_str())["testConfig"];
// ...
或者,如果要顯式檢查testConfig
存在:
// ...
YAML::Node configRoot = YAML::LoadFile(path.c_str());
// do check her as in your code
YAML:Node testConfig = configRoot["testConfig"];
// check if testConfig is a mapping here
YAML::Node includes = testConfig["includes"];
// ...
您正在查看configRoot["includes"]
,但地圖中的頂級鍵是testConfig
。 請改用configRoot["testConfig"]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.