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



有一个带有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标准,则需要在派生结构中定义构造函数。

最新更新