简体   繁体   English

将 Node 转换为 std::string 时出现 yaml-cpp 错误

[英]yaml-cpp error while converting Node to a std::string

I want to convert a Node object from yaml-cpp to a std::string .我想将节点 object 从yaml-cpp转换为std::string I keep getting an error that says it cannot find the right overloaded operator << .我不断收到一个错误,说它找不到正确的重载运算符<< Here's my code:这是我的代码:

#include <iostream>
#include <string>
#include <yaml-cpp/yaml.h>

using namespace std;

int main(int argc, char* argv[]) {
    YAML::Node myNode;
    myNode["hello"] = "world";
    cout << myNode << endl; // That works but I want that not to stdout

    string myString = "";
    myNode >> myString; // Error at this line
    cout << myString << endl;
    return 0;
}

The error is错误是

src/main.cpp:13:12: error: invalid operands to binary expression ('YAML::Node' and 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>>')
[somefile] note: candidate function template not viable: no known conversion from 'YAML::Node' to 'std::byte' for 1st argument
  operator>> (byte  __lhs, _Integer __shift) noexcept
  ^
[A bunch of these 10+]

Using clang++ -std=c++20 -I./include -lyaml-cpp -o prog main.cpp使用clang++ -std=c++20 -I./include -lyaml-cpp -o prog main.cpp

Please help me find out!请帮我找出来!

Actually needed to create an stringstream and then convert it to an normal string:实际上需要创建一个stringstream ,然后将其转换为普通字符串:

#include <iostream>
#include <string>
#include <yaml-cpp/yaml.h>

using namespace std;

int main(int argc, char* argv[]) {
    YAML::Node myNode;
    myNode["hello"] = "world";

    stringstream myStringStream;
    myStringStream << myNode;
    string result = myStringStream.str();

    cout << result << end;
    return 0;
}

Output: Output:

hello: world

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM