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

"Invalid use of member… Did you forget the &?" Accessing class objects in another class obejct

I’m trying to write my own NeuralNetwork, and I use my own Matrix2D class for the operations. I’m trying to write out the values of the neural neutworks first weigth matrix but I get an error like: 161|error: invalid use of member ‘Matrix2D NeuralNewtork::first_hidden_weights()’ (did you forget the ‘&’ ?)|

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

float RandomNumber()
{
    return ((((double) rand() / (RAND_MAX))*2)-1);
}

class Matrix2D{
    public:
        int rows;
        int columns;
        float **matrix;
        Matrix2D(int x, int y){
            rows = x;
            columns = y;
            matrix = new float*[rows];
            for (int i = 0; i < rows; i++) {
                matrix[i] = new float[columns];
            }
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    matrix[i][j] = 0;
                }
            }
        }
        Matrix2D randomizeMatrix(){
            Matrix2D result(rows, columns);
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    matrix[i][j] = RandomNumber();
                }
            }
            return result;
        }
        static Matrix2D scalarMultiply(Matrix2D x, float y){
            Matrix2D result(x.rows, x.columns);
            for (int i = 0; i < x.rows; i++) {
                for (int j = 0; j < x.columns; j++) {
                    result.matrix[i][j] = x.matrix[i][j] * y;
                }
            }
            return result;
        }
        static Matrix2D scalarAddition(Matrix2D x, float y){
            Matrix2D result(x.rows, x.columns);
            for (int i = 0; i < x.rows; i++) {
                for (int j = 0; j < x.columns; j++) {
                    result.matrix[i][j] = x.matrix[i][j] + y;
                }
            }
            return result;
        }
        static Matrix2D scalarSubstraction(Matrix2D x, float y){
            Matrix2D result(x.rows, x.columns);
            for (int i = 0; i < x.rows; i++) {
                for (int j = 0; j < x.columns; j++) {
                    result.matrix[i][j] = x.matrix[i][j] - y;
                }
            }
            return result;
        }
        static Matrix2D matrixAddition(Matrix2D x, Matrix2D y){
            Matrix2D result(x.rows, x.columns);
            for (int i = 0; i < x.rows; i++) {
                for (int j = 0; j < x.columns; j++) {
                    result.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j];
                }
            }
            return result;
        }
        static Matrix2D matrixTranspose(Matrix2D x){
            Matrix2D result(x.columns, x.rows);
            for (int i = 0; i < x.rows; i++) {
                for (int j = 0; j < x.columns; j++) {
                    result.matrix[j][i] = x.matrix[i][j];
                }
            }
            return result;
        }
        static Matrix2D matrixMultiplication(Matrix2D x, Matrix2D y){
            Matrix2D result(x.rows, y.columns);
            for (int i = 0; i < result.rows; i++) {
                for (int j = 0; j < result.columns; j++) {
                    float sum = 0;
                    for (int k = 0; k < x.columns; i++) {
                        sum += x.matrix[i][k] * y.matrix[k][j];
                    }
                    result.matrix[i][j] = sum;
                }
            }
            return result;
        }
        void printMatrix(){
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                cout << matrix[i][j] << " ";
                }
            cout << endl;
            }
            cout << endl;
        }
};

class NeuralNewtork{
    public:
        int numberof_input_nodes;
        int numberof_hidden_layers;
        int numberof_hidden_nodes;
        int numberof_output_nodes;
        Matrix2D first_hidden_weights();
        Matrix2D* hidden_weigths();
        Matrix2D output_weights();
        Matrix2D* hidden_biases();
        Matrix2D output_biases();

        NeuralNewtork(int input_nodes, int hidden_layers, int hidden_nodes, int output_nodes){
            numberof_input_nodes = input_nodes;
            numberof_hidden_layers = hidden_layers;
            numberof_hidden_nodes = hidden_nodes;
            numberof_output_nodes = output_nodes;

            Matrix2D first_hidden_weights(hidden_nodes, input_nodes);
            first_hidden_weights.randomizeMatrix();

            Matrix2D* hidden_weigths[numberof_hidden_layers-1];
                for (int i=0; i<numberof_hidden_layers-1; i++){
                    hidden_weigths[i] = new Matrix2D(numberof_hidden_nodes, numberof_hidden_nodes);
                    hidden_weigths[i]->randomizeMatrix();
                }

            Matrix2D output_weights(numberof_output_nodes, numberof_hidden_nodes);
            output_weights.randomizeMatrix();

            Matrix2D* hidden_biases[numberof_hidden_layers];
                for (int i=0; i<numberof_hidden_layers; i++){
                    hidden_biases[i] = new Matrix2D(numberof_hidden_nodes, 1);
                    hidden_biases[i]->randomizeMatrix();
                }

            Matrix2D output_biases(numberof_output_nodes, 1);
            output_biases.randomizeMatrix();
        }
        Matrix2D feedForward(Matrix2D input){

        }

};

int main()
{
    srand (time(0));
    //Matrix2D myMatrix(3,7);
    //myMatrix.randomizeMatrix();
    //myMatrix.printMatrix();

    NeuralNewtork nn(4, 3, 5, 1);
    nn.first_hidden_weights.printMatrix();     //<=This line gives the error:

    return 0;
}

I’m trying to print out the first_hidden_weight Matrix2D obejcts matrix component to see if my code is working the way it’s intended, but I get an error when I try to access that variable.

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

>Solution :

The first problem is that you defined all your weights as zero-argument functions that return a Matrix2D as opposed to member variables. Drop the ().

Secondly, your constructor creates and initializes shadow variables, which are completely decoupled from your member variables.
Use the = operator to explicitly initialize them:

first_hidden_weights = Matrix2D(hidden_nodes, input_nodes);

For the hidden_weights and hidden_biases members, I suggest you use std::vector<Matrix2D>. This automates memory management and gives you a bunch of handy methods. You might initialize hidden_weights like so:

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)); // or use emplace_back
    hidden_weights.back().randomizeMatrix();
}

Finally, since you seem to call randomizeMatrix a lot after construction, consider making an explicit constructor that automatically randomizes:

enum class Randomize { Randomize };

class Matrix2D {
...
  Matrix2D(int x, int y, Randomize)
  : Matrix2D(x, y) { // delegate to the simple constructor
    randomizeMatrix();
  }
...
};

You can then simply write something like Matrix2D(x, y, Randomize).

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