'how to parse yaml file that contains dictionary and structure at multiple levels in C++
I am trying to parse a yaml file that likes like below in C++:
test_case_0:
- input:
a: 10
b: 5
- output:
area: 50
delta: 5
test_case_1:
- input:
a: 3
b: 7
- output:
area: 21
delta: -4
test_case_2:
- input:
a: 4
b: 4
- output:
area: 16
delta: 0
....
....
so basically top level is dictionary with key ("test_case_<>") mapped to a structure containing a pair of "input" & "output". then in "input", there is dictionary mapping a to a value, and respectively b to a value. same goes for "output".
my intention is to write a for() loop to parse each test_case out, then inside the loop I have a test function that takes in the a & b from "input", calculate the area and delta, then compare with the area & delta from "output".
here is my code:
using namespace YAML;
typedef struct {
int a;
int b;
} tc_input;
typedef struct {
int area;
int delta;
} tc_output;
typedef struct {
tc_input myInput;
tc_output myOutput;
} tc_config;
YAML::Node config = YAML::LoadFile(<my_file_name>);
but I got error if I just try
config["test_case_0"]
error: <user expression 4>:1:7: no viable overloaded operator[] for type 'YAML::Node'
config["test_case_0"]
~~~~~~^~~~~~~~~~~
note: candidate function not viable: no known conversion from 'const char [9]' to 'const YAML::Node' for 1st argument
note: candidate function not viable: no known conversion from 'const char [9]' to 'const YAML::Node' for 1st argument
also got error if I try to parse into my defined structure:
config["test_case_0"].as<tc_config>()
how can I parse each test_case out, and how can I get a count of how many test_case there is in the yaml file?
thanks for helping in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|