Hello everyone. I’d like to kindly share with you a problem that I’m trying to solve while compiling my code. This code calls an external C++ function.
I have recently started using Stan (CmdStan), and I also don’t have a lot of experience with C++. I successfully implemented the Bernoulli example, as shown in the CmdStan User’s Guide. I also managed to compile the code that calls the external C++ function “make_odds”. My next step was then to finally run my own code, but unfortunately it failed. This is my Stan code:
functions {
vector my_function(int n, real k);
}
data {
int n;
vector[n] y;
}
parameters {
real k;
}
transformed parameters {
vector[n] f;
f = my_function(n, k);
}
model {
k ~ normal(1.5, 0.1);
y ~ normal(f, 0.01);
}
And this is my C++ function:
#include <ostream>
#include <cmath>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
template <typename T1, typename T2, typename T3>
T1 my_function(const T2 &n, const T3 &k, std::ostream *pstream__)
{
VectorXd f;
// Do some calculation to find "f"
return f;
}
Then, I try to compile as follows:
make STANCFLAGS=--allow-undefined USER_HEADER=./examples/my_example/my_function.hpp ./examples/my_example/my_stan
Finally, I get this error:
error: no matching function for call to 'my_function'
stan::model::assign(f, my_function(n, k, pstream__),
^~~~~~
././examples/my_example/my_function.hpp:10:4: note: candidate template ignored: couldn't infer template argument 'T1'
T1 my_function(const T2 &n, const T3 &k, std::ostream *pstream__)
^
^Cmake: *** [examples/my_example/my_stan.o] Interrupt: 2
make: *** Deleting intermediate file `examples/my_example/my_stan.hpp'
Does anyone have an idea about what is causing this error? Any help would be very appreciated.
Best regards,
Rodrigo