jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.91k stars 1.78k forks source link

the alias and anchor #1203

Closed tangling123 closed 11 months ago

tangling123 commented 11 months ago

why can't use the iterator to access the alias and anchor?

config.yaml

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: mya_development
  <<: *defaults

and my yaml_test.cpp

#include <iostream>
#include <yaml.h>
using namespace std;

int main() {
    YAML::Node config = YAML::LoadFile("/home/tang/tmp/yaml-cpp/config.yaml");
    for (auto it = config["defaults"].begin(); it != config["defaults"].end(); it++)
        cout << it->first.as<string>() << " " << it->second.as<string>() << endl;

    YAML::Node development = config["development"];
    for (auto it = development.begin(); it != development.end(); it++) {
//        cout << it->first.as<string>() << " " << it->second.as<string>() << endl;  

// the below work 
        if (it->first.as<string>() == "<<"){
            for (auto iter : it->second){
                cout << iter.first.as<string>() << ": " << iter.second.as<string>() << endl;
            }
        }else {
            cout << it->first.as<string>() << " " << it->second.as<string>() << endl;
        }
    }

    return 0;
}

when I'm using cout << it->first.as<string>() << " " << it->second.as<string>() << endl; , wil be Abandoned (core dumped) Could you help me, thanks a lot

jbeder commented 11 months ago

<< for merging is not supported (see #41).

So you're trying to convert a map to a string. Also, in the future, please ask questions like this on Stack Overflow.