c++yamlstdstringyaml-cpp

Getting YAML::TypedBadConversion when trying to read string value with yaml-cpp


I have been having a error that I can't figure out why a getting it. When trying to read the following file example.yaml:

BFS_power:
  graph: power.graph
  type: METIS
BFS_avg:
  graph: DEFAULT
  type: whatever

With the following minimal example:

YAML:Node Instances = YAML::LoadFile(instancesFile);
std::cout << Instances["BFS_power"]["graph"].as<std::string>() << std::endl;

Causes following error:

terminate called after throwing an instance of 'YAML::TypedBadConversion<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
  what():  bad conversion

Doing std::cout << Instances["BFS_power"]["graph"] << std::endl; will print the correct value of power.graph, so I know that the file is being read. However, the object resulting from Instances["BFS_power"]["graph"] is Node not a string.

Is there something that I am missing that is necessary to read strings?


Solution

  • From testing it apparently works if you are saving it in a variable first. So

    YAML:Node Instances = YAML::LoadFile(instancesFile);
    std::cout << Instances["BFS_power"]["graph"].as<std::string>() << std::endl;
    

    doesn't work. But

    YAML:Node Instances = YAML::LoadFile(instancesFile);
    const temp = std::Instances["BFS_power"]["graph"].as<std::string>()
    std::cout << temp << std::endl;
    

    does work.