Hello !
I want to pass a function as parameter in another file.h and it leads to a linker error and I don’t understand why ? any helps !
here’s a part of the code
in ofApp
#include "NeuralNetwork.h"
//...
NeuralNetwork *nn = new NeuralNetwork(2,2,2);
std::vector< float > input = {1, 0};
std::vector< float > targets = {1, 0};
nn->train(input, targets);
in NeuralNetwork.h
I declare/define outside the class the function sigmoid() that I want to pass as param …
#include "Matrix.h"
float sigmoid(float x){
return 1. / (1. + exp(-x));
}
//--------------------------------------------------------------
class NeuralNetwork{
public:
//...
void train(std::vector<float> inputs, std::vector<float> targets_vec){
std::vector< float > outputs_vec = feedforward(inputs);
}
// in the function feedforward() I try to pass the function sigmoid()...
std::vector<float> feedforward(std::vector<float> input_vec){
hidden->map(sigmoid); // hidden is a pointer to a Matrix object
}
then in Matrix.h, I try to use the function
class Matrix{
public:
//...
void map( float (*func)(float) ){
for(int i=0; i < rows_; i++) {
for(int j=0; j < cols_; j++){
float val = data_[i][j];
data_[i][j] = func( val );
}
}
}
};
Thank’s for your help !