1

I have a script that is working fine, but when I try to write a function with the same script I get the error "no instance of overloaded function "aapx" matches the argument list".

I know that an Eigen::Matrix should always be passed by reference to a function so I did and I thought maybe the issue is I am initializing a Eigen::Matrix<std::complex<double> and passing a Eigen::MatrixXcd but that doesn't seem to fix the error either.

The function

void aapx(const Eigen::Ref<const Eigen::MatrixXcd>& uh, Eigen::Ref<const Eigen::MatrixXcd>& vh, Eigen::Ref<const Eigen::MatrixXcd>& ph){ 
//some calculations

}

The main code looks like:

static const int nx = 10;
static const int ny = 10; 

Eigen::Matrix<std::complex<double>, (ny+1), (nx)> uh;
uh.setZero();

Eigen::Matrix<std::complex<double>, (ny+1), (nx)> vh;
vh.setZero();

Eigen::Matrix<std::complex<double>, (ny+1), nx> ph;  
ph.setZero();

aapx(uh,vh,ph); //ERROR

I can post the full code if necessary. The full error:

no instance of overloaded function "aapx" matches the argument list
argument types are: (Eigen::Matrix<std::_Complex<double>, 11, 10, 0, 11, 10>, Eigen::Matrix<std::_Complex<double>, 11, 10, 0, 11, 10>, Eigen::Matrix<std::_Complex<double>, 11, 10, 0, 11, 10>)
Jamie
  • 365
  • 2
  • 5
  • 13
  • The whole code will not be as useful as a *MRE* or minimal reproducible example. Having a working example on compiler explorer (godbolt.org) really helps engagement. You can have library dependencies there too - Eigen is supported. – Something Something Aug 15 '22 at 00:31
  • I dont think that is the full error. I believe there might be more. When this type of error occurs, the compiler prints a list of candidates as well and the reason for rejection. – Something Something Aug 15 '22 at 00:39
  • See here one MRE example: https://godbolt.org/z/dsG5oEPM4 – Something Something Aug 15 '22 at 00:40
  • @MadFred Thanks! I added my full code here, in this link for you to run. Except I am not sure how to include an ``fftw3`` library in an online compiler like this. https://godbolt.org/z/hTzjoETe9 – Jamie Aug 15 '22 at 02:07
  • 1
    @Jamie If you want to have additional libraries for godbolt, you can make a feature request: https://github.com/compiler-explorer/compiler-explorer/issues (Or download it and run it locally) – chtz Aug 15 '22 at 07:14

1 Answers1

2

Your third argument, ph needs to be

Eigen::Ref<const Eigen::MatrixXcd> ph

(without the reference) and not

Eigen::Ref<const Eigen::MatrixXcd>& ph

because ph is already a reference. You do not want to change the reference, you want to change the matrix.

Check out this answer: Correct usage of the Eigen::Ref<> class

Something Something
  • 3,999
  • 1
  • 6
  • 21
  • 1
    It should be either `const Eigen::Ref& ph` (for read-only arguments) or `Eigen::Ref ph` for read-write arguments. – chtz Aug 15 '22 at 06:20
  • 1
    @chtz It's a special case within Eigen since they implement their own types of references. It's explained in the link https://stackoverflow.com/questions/21132538/correct-usage-of-the-eigenref-class – Something Something Aug 15 '22 at 06:30
  • 2
    I know, and to quote from ggael's answer: "So in summary, we could recommend Ref for a writable reference, and const Ref& for a const reference." I.e., replace your `Ref` by `Ref`. – chtz Aug 15 '22 at 07:09
  • 1
    Thanks everyone this seemed to fix the issue. I do have another issue inside the function that has to do with ``ph``, that I will look into. One thing though, what do you mean by read-only arguments? – Jamie Aug 15 '22 at 19:09
  • 1
    @Jamie when you pass by reference you have the possibility to modify the contents of that argument. By making it const you prevent it. – Something Something Aug 16 '22 at 01:19