我刚刚开始使用yaml-cpp,我设法正确地构建了它,并运行了yaml-cpp Wiki中的一些示例,但是我找不到将发射器保存到文件中的方法。
这不可能吗?我的意思是PyYAML库为此提供了一个“转储”功能。yaml-cpp中没有这种功能吗?是否有一些变通办法将yaml发射器转换为stl流,然后将其转储到yaml文件?
请告诉我
谢谢亚当
鉴于此代码:
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 节点“数据”中包含的数据是整数、浮点数还是字符串?注意:我不想检查节点是否是标量、地图、序列等。
上一个答案描述了如何使用确定yaml节点中是否存在密钥YAML::Node::FindValue("parameter").
不幸的是,我不能在最新版本(0.5.1)中调用它:
error: ‘class YAML::Node’ has no member named ‘FindValue’
Run Code Online (Sandbox Code Playgroud)
这是预期可行的还是有一个在最新版本中有效的等效功能?
Is there a way to get the name of a node? For example:
Fruits:
- apple
- orange
- pear
Run Code Online (Sandbox Code Playgroud)
and in C++:
YAML::Node fruit = parser["Fruits"];
cout << fruit.name() << endl; // should print "Fruits"
Run Code Online (Sandbox Code Playgroud)
is there something like YAML::Node::name()? I don't see anything in Node.h that fits the bill.
If there isn't, any suggestions on a simple way to modify the code to record this information?
Thanks!
这是完整的日志:
/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 会在需要的地方自动设置包含目录。
是否可以使用yaml-cpp解析YAML格式的字符串?
没有YAML::Parser::Parser(std::string&)构造函数.(我从http服务器通过libcurl获取YAML字符串.)
我正在尝试使用新版本的libyaml-cpp并遇到链接器问题(undefined reference to 'YAML::LoadFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)').
我按如下方式构建库:
cmake -DBUILD_SHARED_LIBS=ON ..
make
sudo make install
Run Code Online (Sandbox Code Playgroud)
然后我包括yaml-cpp/yaml.h并打电话YAML::LoadFile( some_string );.我的编译行是:
g++ -L/usr/local/lib -I/usr/local/include -lyaml-cpp -std=c++0x -o $@ $^
Run Code Online (Sandbox Code Playgroud)
我已经尝试将确切的.so文件放在那里,但没有运气.使用nm我可以LoadFile在共享库中看到一个函数.如果我以某种方式使用错误的构建行或者库存在问题,我现在无法弄清楚.
我想在我的一个项目中使用 yaml-cpp 来生成 yaml 文件,但是我很难弄清楚我到底应该如何处理这个问题。我需要发出的 yaml 文件应如下所示:
action_counts:
version: 0.3
subtree:
- name: system
local:
- name: adder
action_counts:
- name: add
counts: 1000
- name: idle
counts: 10000
Run Code Online (Sandbox Code Playgroud)
到目前为止我还没能写出最后四行。我知道“-”代表一个数组,但我不知道如何打印名称并计算这样的键数。
我编写了一些代码来试验 yaml-cpp。代码如下:
YAML::Node node;
node["action_counts"] = YAML::Null;
node["action_counts"]["version"] = "0.3";
node["action_counts"]["subtree"].push_back("system");
std::ofstream fout("fileUpdate.yaml");
fout << node;
return 0;
Run Code Online (Sandbox Code Playgroud)
它产生的输出是这样的:
action_counts:
version: 0.3
subtree:
- system
Run Code Online (Sandbox Code Playgroud)
这里的最后一行是错误的,但我还没有设法找到如何打印- name: system而不是这一行。我该怎么做呢?
完成后我如何打印其余部分?是数组local的一部分吗- name?我想一旦我知道如何将它们格式化为两行,我就可以自己弄清楚其余的内容。
与yaml-cpp一起使用的新手。我有一个类似这样的节点:
numbers : [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
在CPP文件中,我想解析为一个向量:
std::vector<int> vi = node["numbers"];
Run Code Online (Sandbox Code Playgroud)
这行不通。除了教程以外,我找不到任何文档,并且该教程中也没有涉及。