使用模板时:错误:没有匹配的构造函数用于初始化

用户名

有一个名称空间detail::RayIntersectorHits

namespace detail {

    template<typename AVertexType, typename AIndexedFaceType, typename ATreeType, typename AVectorType>
    struct RayIntersector {
        using VertexType        = AVertexType;
        using IndexedFaceType   = AIndexedFaceType;
        using TreeType          = ATreeType;
        using VectorType        = AVectorType;

        const std::vector<VertexType>       &vertices;
        const std::vector<IndexedFaceType>  &faces;
        const TreeType                      &tree;

        const VectorType                     origin;
        const VectorType                     dir;
        const VectorType                     invdir;
    };

    template<typename VertexType, typename IndexedFaceType, typename TreeType, typename VectorType>
    struct RayIntersectorHits : RayIntersector<VertexType, IndexedFaceType, TreeType, VectorType> {
        std::vector<igl::Hit>                hits;
    }

}

我正在这样使用detail::RayIntersectorHits

template<typename VertexType, typename IndexedFaceType, typename TreeType, typename VectorType>
inline bool intersect_ray_all_hits(/* input agrs */)
{
    auto ray_intersector = detail::RayIntersectorHits<VertexType, IndexedFaceType, TreeType, VectorType> {
        vertices, faces, tree,
        origin, dir, VectorType(dir.cwiseInverse())
    };
    
    // ...
}

但是我收到此编译错误:

error: no matching constructor for initialization of 'detail::RayIntersectorHits<Matrix<float, 3, 1, 2, 3, 1>, Matrix<int, 3, 1, 2, 3, 1>, Tree<3, float>, Matrix<double, 3, 1, 2, 3, 1> >'
    auto ray_intersector = detail::RayIntersectorHits<VertexType, IndexedFaceType, TreeType, VectorType> {
                           ^                                                                             ~

我不知道如何绕过错误。感谢您的帮助。

解决

错误已通过以下方式解决:

namespace detail {
    template<typename AVertexType, typename AIndexedFaceType, typename ATreeType, typename AVectorType>
    struct RayIntersector {
        using VertexType        = AVertexType;
        using IndexedFaceType   = AIndexedFaceType;
        using TreeType          = ATreeType;
        using VectorType        = AVectorType;

        const std::vector<VertexType>       &vertices;
        const std::vector<IndexedFaceType>  &faces;
        const TreeType                      &tree;

        const VectorType                     origin;
        const VectorType                     dir;
        const VectorType                     invdir;
#ifdef CPP17_NOT_AVAILABLE
        // Aggregate initialization for the derived type is a C++17 feature
        // Trying to compile with C++14
        // If you can't upgrade to C++17 standard, you will need to define a constructor in the derived struct.
        RayIntersector(const std::vector<VertexType>        &vertices
                       , const std::vector<IndexedFaceType>     &faces
                       , const TreeType                         &tree
                       , const VectorType                    origin
                       , const VectorType                    dir
                       , const VectorType                    invdir)
            :
              vertices(vertices)
            , faces(faces)
            , tree(tree)
            , origin(origin)
            , dir(dir)
            , invdir(invdir)
        {}
#endif // CPP17_NOT_AVAILABLE
    };

    template<typename VertexType, typename IndexedFaceType, typename TreeType, typename VectorType>
    struct RayIntersectorHits : RayIntersector<VertexType, IndexedFaceType, TreeType, VectorType> {
        std::vector<igl::Hit>                hits;
#ifdef CPP17_NOT_AVAILABLE
        // Aggregate initialization for the derived type is a C++17 feature
        // Trying to compile with C++14
        // If you can't upgrade to C++17 standard, you will need to define a constructor in the derived struct.
        RayIntersectorHits(const std::vector<VertexType>        &vertices
                           , const std::vector<IndexedFaceType>     &faces
                           , const TreeType                         &tree
                           , const VectorType                    origin
                           , const VectorType                    dir
                           , const VectorType                    invdir)
            : RayIntersector<VertexType, IndexedFaceType, TreeType, VectorType>(vertices, faces, tree, origin, dir, invdir) {}
#endif // CPP17_NOT_AVAILABLE
    };
} // namespace detail

您已经定义了没有任何用户定义的构造函数的聚合结构。您可以struct RayIntersector使用聚合初始化进行初始化

struct A
{
    public:
    int a;
    int b;
};

struct B: public A
{
    int c;
};

void func()
{
    auto varA = A{1,2};   // Compiles with C++14
    auto varB = B{2,3,4}; // Compiles with C++17
}

但是派生类型的聚合初始化是C ++ 17的功能:

聚合初始化的效果是:

从类定义中的数组下标/外观顺序开始,每个直接的公共基础(自C ++ 17起)数组元素或非静态类成员都从初始化程序列表的相应子句中进行复制初始化。

如果无法升级到C ++ 17标准,则需要在派生的struct中定义一个构造函数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有用于初始化可变参数模板类的匹配构造函数

错误:没有匹配函数用于构造函数初始化的调用

C ++错误:没有匹配的构造函数用于初始化

使用 Root/C++ 时出错:“TTree”的初始化没有匹配的构造函数

错误:没有用于初始化“std::shared_ptr<uint8_t []>”的匹配构造函数

在C ++中没有匹配的构造函数来初始化可变参数模板

没有用于初始化“ No_default []”的匹配构造函数

没有用于初始化类的匹配构造函数

在Mac上没有用于初始化的匹配构造函数

C ++没有匹配的构造函数的[]初始化

错误:没有匹配的构造函数来初始化“ set <int>”

创建对象数组,没有匹配的构造函数初始化错误

vscode中的C ++:错误:没有匹配的构造函数来初始化'std :: thread'

基类初始化错误,没有匹配的构造函数

Rcpp:数值积分错误。没有匹配的初始化构造函数

将指针传递给对象时,没有匹配的构造函数初始化问题

c++ 中的初始化没有匹配的构造函数错误。我的构造函数有什么问题?

错误:尝试从向量值构造模板化对象时,没有匹配的构造函数

没有匹配的构造函数,无法使用VSTGUI初始化“ AEffGUIEditor”

尝试实例化模板类的对象时出现“没有匹配的构造函数”错误

没有默认构造函数时,使用垃圾数据初始化对象

每个构造函数成员初始化器列表初始化const数据成员,错误:没有匹配的调用函数

使用成员初始化器列表没有匹配的函数调用错误

具有模板构造函数专门化的模板类,用于初始化模板化的基类

C ++:没有用于初始化的匹配构造函数/候选构造函数不可行:需要单个参数,但未提供任何参数

没有匹配的构造函数来初始化'string'(aka'basic_string <char>')

“没有匹配的初始化构造函数” Rad Studio 10 Clang编译器

为链接列表构建Iterator类(错误:初始化时没有匹配的构造函数)

当类没有constexpr构造函数时,简化冗余的std :: array初始化