1

The OpenFOAM library defines two types, volMesh and surfaceMesh, both of which inherit from GeoMesh<fvMesh>. I want to define a function that accepts an argument:

void foo(GeometricField<vector, fvsPatchField, GeoMesh<fvMesh> >& field) { ... }

However, g++ gives the error "invalid initialization of reference type" when I try to call the function:

// surfaceVectorField is a typedef GeometricField<vector, fvsPatchField, surfaceMesh>
surfaceVectorField Uf( /* initialisation arguments */ ); 
foo(Uf);

Coming from a Java background, this problem seems similar to forgetting to use a declaration such as

void foo(GeometricField<vector, fvsPatchField, ? extends GeoMesh<fvMesh>> field) { ... }

I need to avoid C++11-specific features if possible.

hertzsprung
  • 9,445
  • 4
  • 42
  • 77
  • can you add a bit more code, namely an example of how you try to call your function? – Ashalynd Oct 21 '14 at 15:21
  • 1
    is the error from the function call to `foo()` or the variable definition of `Uf`? http://stackoverflow.com/questions/9285627/is-it-possible-to-pass-derived-classes-by-reference-to-a-function-taking-base-cl – Richard Chambers Oct 21 '14 at 15:37
  • maybe it cannot convert your surfaceMesh parameter to `GeoMesh`, or the whole construct to the reference to the correct complex type. – Ashalynd Oct 21 '14 at 15:40
  • @RichardChambers: The error is from the call to `foo()` – hertzsprung Oct 21 '14 at 15:41
  • Try explicitly converting surfaceMesh to `GeoMesh` when initializing surfaceVectorField. – Ashalynd Oct 21 '14 at 15:43
  • Do you inherit it privately? If yes, it cannot access base in the function call – Kostya Oct 21 '14 at 15:58
  • @Kostya: No, for example, `surfaceMesh` declaration starts like this: `class surfaceMesh : public GeoMesh` – hertzsprung Oct 21 '14 at 16:00
  • Can you make the function a template? `template void foo(GeometricField& field) { ... }` – cdhowie Oct 21 '14 at 16:08
  • Could there be a `const` conflict since the function prototype is not `const`? [Another "invalid initialization of reference of type" error](http://stackoverflow.com/questions/11609965/another-invalid-initialization-of-reference-of-type-error) or [g++ gives error: invalid initialization of reference of type](http://stackoverflow.com/questions/17453738/g-gives-error-invalid-initialization-of-reference-of-type-char-from-expre) – Richard Chambers Oct 21 '14 at 17:27

1 Answers1

0

The basic problem in your function declaration is that the type of the argument can not be resolved. That means that the specilization of the template class can not be deduced if you give the base class as a template parameter.

The argument you hope to take in is an instance of the template class GeometricField. Further, the name of the class template is not a type. Indicating that a function definition foo(GeometricField& field); is not possible.

If you want the function to accept geometric fields from both surface and volumetric mesh types you need to make it a template function or overload it. First template:

template <typename T>
void foo(GeometricField<vector, fvsPatchField, T>& field) {}

or just

template <typename T>
void foo(T& field) {}

which ever better suits what you do in the function. Notice also that if the function is going to be reused, you need to take care of the cases when the argument is not what you expect.

Overloading:

void foo(surfaceVectorField& field);
void foo(volVectorField& field);

Both solutions should get you there.

PekkaRo
  • 80
  • 8