我想在不知道密钥的情况下获取地图中的每个节点.
我的YAML看起来像这样:
characterType :
type1 :
attribute1 : something
attribute2 : something
type2 :
attribute1 : something
attribute2 : something
Run Code Online (Sandbox Code Playgroud)
我不知道将声明多少"类型"或这些键的名称是什么.这就是我试图遍历地图的原因.
struct CharacterType{
std::string attribute1;
std::string attribute2;
};
namespace YAML{
template<>
struct convert<CharacterType>{
static bool decode(const Node& node, CharacterType& cType){
cType.attribute1 = node["attribute1"].as<std::string>();
cType.attribute2 = node["attribute2"].as<std::string>();
return true;
}
};
}
---------------------
std::vector<CharacterType> cTypeList;
for(YAML::const_iterator it=node["characterType"].begin(); it != node["characterType"].end(); ++it){
cTypeList.push_back(it->as<CharacterType>());
}
Run Code Online (Sandbox Code Playgroud)
以前的代码在编译时没有给出任何麻烦,但是在执行时我得到了这个错误:在抛出一个实例后调用terminate YAML::TypedBadConversion<CharacterType>
我也尝试使用子索引而不是迭代器,得到相同的错误.
我确定我做错了什么,我只是看不到它.
我正在尝试编译我自己的c ++/qt5.2应用程序,该应用程序在Ubuntu 14.04下使用yaml-cpp.
发出make命令时,我收到以下错误
fatal error: yaml-cpp/yaml.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)
在线上:
#include <yaml-cpp/yaml.h>
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我最近决定使用yaml作为我的配置文件技术,并且正在OpenSuse 11.3上编写一个c ++ linux应用程序.
问题是,即使在成功安装cmake的,如图所示,YAML的CPP文档页面上compilling YAML-CPP,我仍然无法编译YAML-CPP页面上简单的演示程序在这里.
例如,当我尝试编译monster.yaml和main.cpp示例时
我的编译器在发出命令gcc main.cpp时发出以下错误:
main.cpp:24:25:错误:'YAML'没有命名类型
main.cpp:24:35:错误:在'&'标记
main.cpp 之前的预期unqualified-id :24:35:错误:预期') 'before'&'token
main.cpp:24:35:错误:'&'令牌
main.cpp 之前的预期初始化程序:30:25:错误:'YAML'没有命名类型
main.cpp:30:35:错误:'&'令牌
main.cpp 之前的预期unqualified-id :30:35:错误:预期')'在'''之前'令牌
main.cpp:30:35:错误:'&'令牌
main.cpp 之前的预期初始值设定项: 35:25:错误:'YAML'没有命名类型
main.cpp:35:35:错误:在'&'令牌
main.cpp:35:35:错误:预期')之前预期的unqualified-id' ''' 'token
main.cpp:35:35:错误:'&'令牌之前的预期初始值设定项
我尝试将include指令更改#include "yaml-cpp/yaml.h"为#include <yaml.h>,因为我已经安装了yaml lib,但这并没有解决任何问题.
那我做错了什么?
以下是从第24到40行粘贴的有问题的代码:
void operator >> (const YAML::Node& node, Vec3& v) {
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
void operator >> (const YAML::Node& node, Power& power) {
node["name"] >> power.name;
node["damage"] >> power.damage; …Run Code Online (Sandbox Code Playgroud) 我是yaml-cpp的新手,我尝试了最简单的程序,但失败了,也没有找到作者的google/github网站的答案.
#include <iostream>
#include "yaml-cpp/yaml.h"
using namespace std;
int main()
{
YAML::Node config = YAML::LoadFile("sample.yaml");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
来自YAML官方网站的sample.yaml样本
--- !clarkevans.com/^invoice
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description … 我知道在解析期间,yaml-cpp 可以抛出一个异常,该异常具有有关异常发生位置的良好文件位置(行号和列号)信息。后期解析怎么样?是否有任何机制可以确定与给定节点关联的 YAML 文件的(第一)行号?
我正在尝试解析yaml usign yaml-cpp.这是我的yaml:
---
configuration:
- height: 600
- widht: 800
- velocity: 1
- scroll: 30
types:
- image: resources/images/grass.png
name: grass
- image: resources/images/water.png
name: water
version: 1.0
Run Code Online (Sandbox Code Playgroud)
当我做
YAML::Node basenode = YAML::LoadFile("./path/to/file.yaml");
int height;
if(basenode["configuration"])
if(basenode["configuration"]["height"]
height = basenode["configuration"]["height"].as<int>();
else
cout << "The node height doesn't exist" << endl;
else
cout << "The node configuration doesn't exist" << endl;
Run Code Online (Sandbox Code Playgroud)
我收到消息:"节点高度不存在".我如何访问该字段(以及其他字段?)
非常感谢!
我在我的项目中使用 yaml-cpp 来处理各种事情。现在我想写出一些数据作为 JSON。由于 JSON 是 YAML 的子集,至少对于我需要的功能,我理解应该可以在 yaml-cpp 中设置一些选项来输出纯 JSON。这是怎么做的?
鉴于此代码:
void LoadFromYaml(const YAML::Node& node){
const YAML::Node& Data=node["Data"];
if(Data){
if(Data.ValueIsInt)// Do something with integer.
if(Data.ValueIsFloat)// Do something with float.
if(Data.ValueIsString)// Do something with string.
}
}
Run Code Online (Sandbox Code Playgroud)
如何检查 YAML 节点“数据”中包含的数据是整数、浮点数还是字符串?注意:我不想检查节点是否是标量、地图、序列等。
这是完整的日志:
/tmp/ccCvErNZ.o: In function `YAML::detail::node& YAML::detail::node_data::get<std::string>(std::string const&, std::shared_ptr<YAML::detail::memory_holder>)':
cricket.cpp:(.text._ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0x94): undefined reference to `YAML::detail::node_data::convert_to_map(std::shared_ptr<YAML::detail::memory_holder>)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我试图编译的代码很简单
#include <iostream>
#include <yaml-cpp/yaml.h>
using namespace std;
int main() {
YAML::Node test = YAML::LoadFile("test.yaml");
if (test["date"]) {
cout << "HELLO";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用的 YAML 是来自http://www.yaml.org/start.html的示例
如果我只是尝试加载 YAML,它加载得很好。但是,如果我尝试访问任何数据,则会出现相同的错误。所以这不是链接问题。
编辑:我可以做这样的事情cout << test,并cout << test.type()和其他功能。我认为问题在于使用基于字符串的映射来访问内部节点。
我已将yaml-cppgit 存储库添加为子模块,并使用add_subdirectory.
一切都很好,但我必须设置YAML_CPP_INCLUDE_DIR并YAML_CPP_LIBRARIES手动将它们用于我自己的目标。
因为有一个文件yaml-cpp-config.cmake(在 build 文件夹中生成)设置了这些变量,所以我试图只包含它:
include("${CMAKE_BINARY_DIR}/yaml-cpp/yaml-cpp-config.cmake")
Run Code Online (Sandbox Code Playgroud)
但后来我得到:
CMake Error at /bla/bla/build/yaml-cpp/yaml-cpp-config.cmake:11 (include):
The file
/bla/bla/aml-cpp/yaml-cpp-targets.cmake
was generated by the export() command. It may not be used as the argument
to the include() command. Use ALIAS targets instead to refer to targets by
alternative names.
Run Code Online (Sandbox Code Playgroud)
我真的不明白这个消息。如何在yaml-cpp不设置硬编码变量的情况下为我的目标提供包含目录和库?
我不是在寻找一种方法来正确地保存 include()文件,以防它不必完成。我只是有兴趣我怎么应该提供给我的目标所需的信息。
不幸的是,yaml-cpp似乎没有使用target_include_directories()which 会在需要的地方自动设置包含目录。