c++ - 错误:未定义模板的隐式实例化 'llvm::yaml::MissingTrait

标签 c++ templates yaml llvm

我正在开发一个使用 LLVM YAML I/O 库的项目。这是我正在关注的文档/教程:

我正在尝试复制您在 llvm::yaml::MappingTraits 上为 struct 数据类型定义特化的示例。此示例位于页面顶部。

这是我编写的代码:

#include <cstdlib>  /* for EXIT_FAILURE */
#include <string>
#include <vector>

#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLParser.h"

using std::string;
using std::vector;

using llvm::outs;
using llvm::errs;

using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::MappingTraits;
using llvm::yaml::IO;
using llvm::yaml::Input;
using llvm::yaml::Output;

struct Person {
    string name;
    int hatSize;
};

template <>
struct MappingTraits<Person> {
    static void mapping(IO& io, Person& info) {
        io.mapRequired("name", info.name);
        io.mapOptional("hat-size", info.hatSize);
    }
};

int main(int argc, const char **argv) {
    Person tom;
    tom.name = "Tom";
    tom.hatSize = 8;
    Person dan;
    dan.name = "Dan";
    dan.hatSize = 7;
    std::vector<Person> persons;
    persons.push_back(tom);
    persons.push_back(dan);

    Output yout(llvm::outs());
    yout << persons;

    return EXIT_SUCCESS;
}

在我看来,我已经完全复制了他们在该教程中的示例代码。但是当我尝试编译程序(使用 makefile)时,我收到了这个神秘的错误消息:

clang++ -I/usr/local/include -std=c++11   -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -std=c++14 -fcxx-exceptions -g -Wall   -c -o yaml_project.o yaml_project.cpp
In file included from yaml_project.cpp:12:
/usr/local/include/llvm/Support/YAMLTraits.h:1871:36: error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait<std::vector<Person, std::allocator<Person> > >'
  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
                                   ^
yaml_project.cpp:153:10: note: in instantiation of function template specialization 'llvm::yaml::operator<<<std::vector<Person, std::allocator<Person> > >' requested here
    yout << persons;
         ^
/usr/local/include/llvm/Support/YAMLTraits.h:307:8: note: template is declared here
struct MissingTrait;
       ^
1 error generated.
<builtin>: recipe for target 'yaml_project.o' failed
make: *** [yaml_project.o] Error 1

我不认为错误出在我用来编译这个程序的命令中,因为它之前对我有用,可以将 LLVM 库编译并链接到我的可执行文件中。我认为问题出在代码中,但我无法确定是什么。

提到的头文件 llvm/Support/YAMLTraits.h 的代码在这里:

https://llvm.org/doxygen/YAMLTraits_8h_source.html

最佳答案

阅读文档,在我看来,支持您的特定 vector<Person>需要注册宏:

LLVM_YAML_IS_SEQUENCE_VECTOR(Person)
// or
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Person)

参见实用程序宏:https://llvm.org/docs/YamlIO.html#id22

关于c++ - 错误:未定义模板的隐式实例化 'llvm::yaml::MissingTrait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67291669/

相关文章:

c++ - 我正在尝试将 2005 时代的库与 MSVC 2013 一起使用,但我在 Release模式下遇到 Unresolved external 错误

c++ - 存储指向对象成员变量的指针

go - 问题解析yaml文件

php - 自定义表格的MySQL表结构

c++ - 编译器如何知道 C++ constexpr 计算不会触发未定义的行为?

c++ - FSCTL_LOOKUP_STREAM_FROM_CLUSTER 返回 ERROR_INVALID_PARAMETER

c++ - 在压缩和未压缩数据之间切换

c++ - 派生模板类访问基类成员数据

templates - Kotlin 中 Float 和 Double 之间共享扩展函数的实现

ruby-on-rails - 更改 YAML 文件而不重新启动 Rails 4