错误 C2338:YOU_MIXED_DIFFERENT_NUMERIC_TYPES

error C2338: YOU_MIXED_DIFFERENT_NUMERIC_TYPES

我在 Eigen 库的第 eigen/src/Core/AssignEvaluator.h(834) 行收到此错误:

error C2338: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY

根据编译器日志,我认为错误是由代码中的这一行触发的:

class IndexedMesh {
    
    const TriangleMesh* m_tm;

    // ...
}

Vec3d IndexedMesh::normal_by_face_id(int face_id) const {
    return m_tm->normal_by_face_id(face_id); // => Error is thrown here
}

Vec3f TriangleMesh::normal_by_face_id(int face_id) const
{
    return Vec3f();
}

完整的编译日志是这样的:

c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/AssignEvaluator.h(834): error C2338: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/PlainObjectBase.h(732): note: see reference to function template instantiation 'void Eigen::internal::call_assignment_no_alias<Derived,Eigen::Matrix<float,3,1,2,3,1>,Eigen::internal::assign_op<double,float>>(Dst &,const Src &,const Func &)' being compiled
        with
        [
            Derived=Eigen::Matrix<double,3,1,2,3,1>,
            Dst=Eigen::Matrix<double,3,1,2,3,1>,
            Src=Eigen::Matrix<float,3,1,2,3,1>,
            Func=Eigen::internal::assign_op<double,float>
        ]
c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/PlainObjectBase.h(537): note: see reference to function template instantiation 'Derived &Eigen::PlainObjectBase<Derived>::_set_noalias<Eigen::Matrix<float,3,1,2,3,1>>(const Eigen::DenseBase<Eigen::Matrix<float,3,1,2,3,1>> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<double,3,1,2,3,1>
        ]
c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/PlainObjectBase.h(537): note: see reference to function template instantiation 'Derived &Eigen::PlainObjectBase<Derived>::_set_noalias<Eigen::Matrix<float,3,1,2,3,1>>(const Eigen::DenseBase<Eigen::Matrix<float,3,1,2,3,1>> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<double,3,1,2,3,1>
        ]
c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/Matrix.h(378): note: see reference to function template instantiation 'Eigen::PlainObjectBase<Eigen::Matrix<double,3,1,2,3,1>>::PlainObjectBase<Derived>(const Eigen::DenseBase<Derived> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<float,3,1,2,3,1>
        ]
c:\users\m3\repos\qt3d-editor\editorlib\deps\eigen\eigen\src/Core/Matrix.h(377): note: see reference to function template instantiation 'Eigen::PlainObjectBase<Eigen::Matrix<double,3,1,2,3,1>>::PlainObjectBase<Derived>(const Eigen::DenseBase<Derived> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<float,3,1,2,3,1>
        ]
..\..\qt3d-editor\editorlib\src\libslic3r\SLA\IndexedMesh.cpp(119): note: see reference to function template instantiation 'Eigen::Matrix<double,3,1,2,3,1>::Matrix<Derived>(const Eigen::EigenBase<Derived> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<float,3,1,2,3,1>
        ]
..\..\qt3d-editor\editorlib\src\libslic3r\SLA\IndexedMesh.cpp(119): note: see reference to function template instantiation 'Eigen::Matrix<double,3,1,2,3,1>::Matrix<Derived>(const Eigen::EigenBase<Derived> &)' being compiled
        with
        [
            Derived=Eigen::Matrix<float,3,1,2,3,1>
        ]

Vec3dVec3f 之间的类型不匹配是错误原因。

// Caller of the method:
Vec3d IndexedMesh::normal_by_face_id(int face_id) const {
    return m_tm->normal_by_face_id(face_id);
}

// Bad method:
Vec3f TriangleMesh::normal_by_face_id(int face_id) const
{
    return Vec3f();
}

// Fine method:
Vec3d TriangleMesh::normal_by_face_id(int face_id) const
{
    return Vec3d();
}