D. Mogwitz
D. Mogwitz

Reputation: 35

Overloading function is ambigious

My problem

I have this both function headers:

const FlowTools::LocalFlow < double >& harmonic( const int i, const int j, const int k ) const;

const FlowTools::LocalFlow < std::complex < double > >& harmonic( const int i, const int j, const int k, const int f ) const;

LocalFlow is just a class which holds five physical parameters (velocity 3D, density and pressure). I want to get just a double value if there is no frequency parameter in the header. The direct component is also indicate if you are write f=0 in the header, but in this case I get a complex value. I would not like to edit the whole program like this: xxx.harmonic().real(). Before overloading the function it calls flowField(int i, int j, int k). I do refactoring.

Now I am testing the function with Google Test (unit testing library for C++) like this:

ASSERT_THROW(xxx.harmonic(d,d,d), ERROR);

ASSERT_THROW(xxx.harmonic(d,d,d,0), ERROR);

... And get this error for the first function:

'ASSERT_THROW( xxx.harmonic(d,d,d), ERROR)' is ambiguous ' Candidates are:

FlowTools::LocalFlow<double> & harmonic(int, int, int)

FlowTools::LocalFlow<std::complex<double>> & harmonic(int, int, int, int) '

What is my mistake?

Upvotes: 1

Views: 97

Answers (1)

Jarod42
Jarod42

Reputation: 217358

Remove f = 0 so it would no longer be ambiguous.

So instead of

const FlowTools::LocalFlow<double>&
harmonic(int i, int j, int k) const;

const FlowTools::LocalFlow<std::complex<double>>&
harmonic(int i, int j, int k, int f = 0) const;
// f = 0 make ambiguous call when not provided

use

const FlowTools::LocalFlow<double>&
harmonic(int i, int j, int k) const;

const FlowTools::LocalFlow<std::complex<double>>&
harmonic(int i, int j, int k, int f) const;

Upvotes: 1

Related Questions