Home > Net >  Using a classes object with constructor in another class. error : no matching function for call
Using a classes object with constructor in another class. error : no matching function for call

Time:11-11

Here's the error message: main.cpp|119|error: no matching function for call to 'Matrix2D::Matrix2D()'|

My guess is that when declaring Matrix2D first_hidden_weights in class NeuralNewtork it required 2 parameters beacuse of the class Matrix2D-s constructor. But I cannot give it the parameters because I don't know them yet.

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

using namespace std;

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

class Matrix2D{
    public:
        int rows;
        int columns;
        vector<vector<float> > matrix;
        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);
            }
        }
        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;
        vector<Matrix2D> hidden_weights;
        Matrix2D output_weights;
        vector<Matrix2D> hidden_biases;
        Matrix2D output_biases;

        NeuralNewtork(int input_nodes, int hidden_layers, int hidden_nodes, int output_nodes){ // This line
               //gives 3 errors, all of them the same.
            numberof_input_nodes = input_nodes;
            numberof_hidden_layers = hidden_layers;
            numberof_hidden_nodes = hidden_nodes;
            numberof_output_nodes = output_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();
            }

            output_weights = Matrix2D(numberof_output_nodes, numberof_hidden_nodes);
            output_weights.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();
            }

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

        }

};

CodePudding user response:

The problem is that when you define other constructors(other than default constructor) for your class then the default constructor is not automatically generated by the compiler.

To solve the mentioned error just add any one of the below shown constructor:

 Matrix2D() = default;

or

Matrix2D()
{
    std::cout<<"default constructor"<<std::endl;
}

into your Matrix2D class definition. Both of the above will work.

Mistake 2

Your feedForward function doesn't return anything. You should add a return statement inside its body.

Matrix2D feedForward(Matrix2D input)
{
     return Matrix2D(); //added this return
}
  • Related