Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot call non-static function on a class object in a vector

I’m bulding my own Neural Network with my own Matrix class.

I’m trying to use the swishMatrix() function on a Matrix2D class object, before adding it to a vector<Matrix2D> variable.
But I get this error and I have no idea why. -> no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|

When I use the swishMatrix() on a normal Matrix2D object it works fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the Matrix2D class

class Matrix2D{
    public:
        int rows;
        int columns;
        vector<vector<float> > matrix;
        Matrix2D() = default;
        Matrix2D(int x, int y){
            rows = x;
            columns = y;
            for (int i = 0; i < rows; i++) {
                vector<float> v1;
                for (int j = 0; j < columns; j++) {
                    v1.push_back(0);
                }
                matrix.push_back(v1);
            }
        }
        swishMatrix(){            
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    matrix[i][j] = matrix[i][j] * sigmoid(matrix[i][j]);
                }
            }            
        }        
        //Here there's a lot of static functions for matrix operations
};

Here’s the Neural Network class

class NeuralNewtork{
    public:
       
        //A lot more declaration here but not important
        Matrix2D first_hidden_weights;

        Matrix2D input_nodes;

        vector<Matrix2D> hidden_weights;      

        vector<Matrix2D> hidden_biases; 

        vector<Matrix2D> activated_hidden_nodes;       

        NeuralNewtork(int input_nodes, int hidden_layers, int hidden_nodes, int action_nodes){
            first_hidden_weights = Matrix2D(numberof_hidden_nodes, numberof_input_nodes);
            first_hidden_weights.randomizeMatrix();            

            hidden_weights.reserve(numberof_hidden_layers-1);
            for (int i=0; i<numberof_hidden_layers-1; i++){
                hidden_weights.push_back(Matrix2D(numberof_hidden_nodes, numberof_hidden_nodes));
                hidden_weights.back().randomizeMatrix();
            }           

            hidden_biases.reserve(numberof_hidden_layers);
            for (int i=0; i<numberof_hidden_layers; i++){
                hidden_biases.push_back(Matrix2D(numberof_hidden_nodes, 1));
                hidden_biases.back().randomizeMatrix();
            }     
            //There are more declerations here but they aren't important for this problem   
        }

        feedForward(Matrix2D input){
            input_nodes = input;

        
            for(int i = 0; i < numberof_hidden_layers+1; i++){
                if(i==0){                    
                    activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(first_hidden_weights, input_nodes), hidden_biases[0]).swishMatrix());
                    //This is the line where I get the error
                    //no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
                    
                }
                if(i!=0 && i!=numberof_hidden_layers){
                    activated_hidden_nodes.push_back(Matrix2D::matrixAddition(Matrix2D::matrixMultiplication(hidden_weights[i-1], activated_hidden_nodes[i-1]), hidden_biases[i]).swishMatrix());  
                    //This is also a line where I get the error
                    //no matching function for call to 'std::vector<Matrix2D>::push_back(int)'|
                }
                if(i==numberof_hidden_layers){
                    //Not important
                }
            }
        }

I might have missed some part of the code, it was hard to keep short, but all the needed variables are correctly assigned.

>Solution :

The trivial fix is to make swishMatrix return the matrix object again:

Matrix2D& swishMatrix(){            
    for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
              matrix[i][j] = matrix[i][j] * sigmoid(matrix[i][j]);
         }
    }     
    return *this;       
}  

Never ever write code again without explicit return types on your methods.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading