0

The error that im getting is randm.cpp:42:52: error: no matching function for call to ‘Neuron::activationFunc(double&, double&)’ neu.activationFunc(feature[2][4], predicted[2][1]);

Is it possible to pass arrays or will i have to find another way of doing it? Here is my code. Its been a few years since i used classes so im a tad bit rusty.

#include <fstream> #include <sstream> #include <iostream> #include <cstdlib> using namespace std; int correct = 0; class Neuron { //double (&features)[150][4]; public: double alpha = 0.5; double w1 = 1.0; double w2 = -1; double w3 = 0.5; double w4 = 0; double x = 0; int theta = 1; int hardLim = 0; int track = 0; //void testClass(); void activationFunc(double feature[2][4], double predicted[2][1]); }; void Neuron::activationFunc (double feature[2][4], double predicted[2][1]) { for (int i = 0; i < 2; i++) { x = (w1*feature[i][0]) + (w2*feature[i][1]) + (w3*feature[i][2]) + (w4*feature[i][3]) + predicted[2][1]; cout <<"\n X value is: " << x << endl; //hardLimit(x); //track = i; } } int main(int argc, char* argv[]) { //feature = col 1-4, predicted = col 5 double feature[2][4]={1,2,3,4,5,6,7,8}; double predicted[2][1]={4,5}; Neuron neu; neu.activationFunc(feature[2][4], predicted[2][1]); }

Thanks. P.S. I know there are a few things in my code that arent used yet.

4
  • Put your arrays inside a struct, then pass the struct. struct Feature { double value[2][4]; }; and struct Predicted { double value[2][1]; }; Commented Apr 14, 2019 at 19:26
  • Possible duplicate of Passing a 2D array to a C++ function Commented Apr 14, 2019 at 19:41
  • Turn on more warnings. You are accessing elements outside your array-bounds. predicted[2][1] Commented Apr 14, 2019 at 19:43
  • neu.activationFunc(feature[2][4], predicted[2][1]); You are not passing a multi-dim array but you are only passing an element. to do so pass only the name of the array: neu.activationFunc(feature, predicted);. ` Commented Apr 14, 2019 at 19:52

2 Answers 2

1

To make it compile you need to change this line:

neu.activationFunc(feature[2][4], predicted[2][1]);

so that it looks like this instead:

neu.activationFunc(feature, predicted);
Sign up to request clarification or add additional context in comments.

1 Comment

Note that you omit the “array sizes” here because they would be subscripts instead (to pick a single element, although these are of course out of range).
0

You can pass the multi-dimension array by reference or by pointer:

Your method prototype should look like:

class Neuron {
    //...
    void activationFunc(double(&)[2][4], double(&)[2][1]);
};

And the definition:

void Neuron::activationFunc (double (&feature)[2][4], double (&predicted)[2][1]) {
    for (size_t i{}; i != 2; ++i) {
        x = (w1 * feature[i][0]) + (w2 * feature[i][1]) + 
            (w3 * feature[i][2]) + (w4 * feature[i][3]) + predicted[1][0];
        std::cout << "X value is: " << x << std::endl;
    }

}

And in main just pass the arrays names:

neu.activationFunc(feature, predicted);
  • There is an Undefined Behavior in your loop:

    x = (w1*feature[i][0]) + (w2*feature[i][1]) + (w3*feature[i][2]) + (w4*feature[i][3]) + predicted[2][1];
    

predicted is an array of [2][1] dimension so the valid range is [1][0] not [2][1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.