尝试使用 yaml-cpp 读取字符串值时获取 YAML::TypedBadConversion

发布于 2025-01-15 10:30:14 字数 843 浏览 0 评论 0原文

我一直有一个错误,我不明白为什么会得到它。当尝试读取以下文件 example.yaml 时:

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

使用以下最小示例:

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

导致以下错误:

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

执行 std::cout <<实例["BFS_power"]["graph"] << std::endl; 将打印 power.graph 的正确值,因此我知道该文件正在被读取。但是,Instances["BFS_power"]["graph"] 生成的对象是 Node 而不是字符串。

我是否缺少读取字符串所必需的东西?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2025-01-22 10:30:14

从测试来看,如果您首先将其保存在变量中,它显然可以工作。所以

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

不起作用。但

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

确实有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文