代码之家  ›  专栏  ›  技术社区  ›  Robert Boehne

无法读取嵌套映射(引发yaml::invalidscalar)

  •  1
  • Robert Boehne  · 技术社区  · 16 年前

    我有一个类(包含一些标量值和浮点向量),我想读写一个实例作为另一个映射的值。

        
    // write
    
     out << YAML::Key << "my_queue" << YAML::Value << my_queue;
    
    
    
    // read (other code cut out...)
     for (YAML::Iterator it=doc.begin();it!=doc.end();++it)
      {
        std::string key, value;
        it.first() >> key;
        it.second() >> value;
        if (key.compare("my_queue") == 0) {
          *it >> my_queue;
        }
     }
    

    Writing this class works perfectly, but I can't seem to read it no matter what I do. It keeps throwing an InvalidScalar.

    Caught YAML::InvalidScalar  yaml-cpp: error at line 20, column 13: invalid scalar
    

    这就是输出(用yaml cpp编写而不报告任何错误)如下所示:

    Other Number: 80
    my_queue:
      size: 20
      data:
        - 3.5
        - -1
        - -1.5
        - 0.25
        - -24.75
        - -5.75
        - 2.75
        - -33.55
        - 7.25
        - -11
        - 15
        - 37.5
        - -3.75
        - -28.25
        - 18.5
        - 14.25
        - -36.5
        - 6.75
        - -0.75
        - 14
      max_size: 20
      mean: -0.0355586
      stdev: 34.8981
    even_more_data: 1277150400
    

    文档似乎说这是支持的用法,一个嵌套的映射,在本例中,序列作为值之一。它抱怨它是一个无效的标量,即使我做的第一件事是告诉它这是一个映射:

    YAML::Emitter& operator << ( YAML::Emitter& out, const MeanStd& w )
    {
      out << YAML::BeginMap;
      out << YAML::Key << "size";
      out << YAML::Value << w.size();
    
      out << YAML::Key << "data";
      out << YAML::Value << YAML::BeginSeq;
      for(Noor::Number i=0; i<w.size(); ++i) {
        out << w[i];
      }
      out << YAML::EndSeq;
      out << YAML::Key << "max_size";
      out << YAML::Value << w.get_max_size();
      out << YAML::Key << "mean";
      out << YAML::Value << w.mean();
      out << YAML::Key << "stdev";
      out << YAML::Value << w.stdev();
    
      out << YAML::EndMap;
      return out;
    }
    

    有人看到这个问题吗?

    1 回复  |  直到 16 年前
        1
  •  1
  •   Jesse Beder    16 年前

    当你读山药时:

    std::string key, value;
    it.first() >> key;
    it.second() >> value; // ***
    if (key.compare("my_queue") == 0) {
      *it >> my_queue;
    }
    

    标记的行试图读取 价值 作为标量的键/值对( std::string )这就是为什么它告诉你它是一个无效的标量。相反,您需要:

    std::string key, value;
    it.first() >> key;
    if (key.compare("my_queue") == 0) {
      it.second() >> my_queue;
    } else {
      // ...
      // for example: it.second() >> value;
    }