error: invalid initialization of reference of type 'Type&' from expression of type ‘const Type’

在编译时遇到下面的问题:

fvPatchFields/solidContact/solidContactFvPatchVectorField.C:932:11:   required from here
/home/q83372/OpenFOAM/OpenFOAM-v1912/src/OpenFOAM/lnInclude/GGIInterpolationWeights.C:612:68: error: invalid initialization of reference of type ‘Foam::labelField& {aka Foam::Field<int>&}’ from expression of type ‘const Foam::Field<int>’
     labelField& patchFaceNonOverlapAddr = tpatchFaceNonOverlapAddr();

修改方式:

tpatchFaceNonOverlapAddr() 修改为 tpatchFaceNonOverlapAddr.ref() 就可以了。

主要原因是
FieldFunctions.H 中定义的 Foam::T() 是:

    template<class Type> void T(Field<Type>& res, const UList<Type>& f);

然而,类 dimensionedField<Type, GeoMesh> 的成员函数 T() 定义为:

     template<class Type, class GeoMesh> tmp<DimensionedField<Type, GeoMesh>> DimensionedField<Type, GeoMesh>::T() const
    {
        tmp<DimensionedField<Type, GeoMesh>> result
        (
            DimensionedField<Type, GeoMesh>::New
            (
                name() + ".T()",
                mesh_,
                dimensions_
            )
        );
    Foam::T(result(), *this);
    return result;
    }

operator() of the tmp 返回 a const reference of T 而不是 Foam::T() 要求的 a non-const reference .

Reference:

  1. 0003564: compiled errors while using DimensionedField<Type, GeoMesh> - OpenFOAM Issue Tracking