Использование объекта классов с конструктором в другом классе. ошибка : нет соответствующей функции для вызова

#c #class #constructor

Вопрос:

Вот сообщение об ошибке: main.cpp|119|error: no matching function for call to 'Matrix2D::Matrix2D()'|

Я предполагаю, что при объявлении Matrix2D first_hidden_weights в class NeuralNewtork нем требовалось 2 параметра из-за конструктора class Matrix2D -s. Но я не могу дать ему параметры, потому что я их еще не знаю.

 #include lt;stdlib.hgt; #include lt;time.hgt; #include lt;vectorgt;  using namespace std;  float RandomNumber() {  return ((((double) rand() / (RAND_MAX))*2)-1); }  class Matrix2D{  public:  int rows;  int columns;  vectorlt;vectorlt;floatgt; gt; matrix;  Matrix2D(int x, int y){  rows = x;  columns = y;  for (int i = 0; i lt; rows; i  ) {  vectorlt;floatgt; v1;  for (int j = 0; j lt; columns; j  ) {  v1.push_back(0);  }  matrix.push_back(v1);  }  }  Matrix2D randomizeMatrix(){  Matrix2D result(rows, columns);  for (int i = 0; i lt; rows; i  ) {  for (int j = 0; j lt; 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 lt; x.rows; i  ) {  for (int j = 0; j lt; 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 lt; x.rows; i  ) {  for (int j = 0; j lt; 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 lt; x.rows; i  ) {  for (int j = 0; j lt; 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 lt; x.rows; i  ) {  for (int j = 0; j lt; 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 lt; x.rows; i  ) {  for (int j = 0; j lt; 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 lt; result.rows; i  ) {  for (int j = 0; j lt; result.columns; j  ) {  float sum = 0;  for (int k = 0; k lt; 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 lt; rows; i  ) {  for (int j = 0; j lt; columns; j  ) {  cout lt;lt; matrix[i][j] lt;lt; " ";  }  cout lt;lt; endl;  }  cout lt;lt; endl;  } };  class NeuralNewtork{  public:  int numberof_input_nodes;  int numberof_hidden_layers;  int numberof_hidden_nodes;  int numberof_output_nodes;  Matrix2D first_hidden_weights;  vectorlt;Matrix2Dgt; hidden_weights;  Matrix2D output_weights;  vectorlt;Matrix2Dgt; 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; ilt;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; ilt;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){   }  };  

Комментарии:

1. Прочитайте о списке инициализаторов в вашей любимой книге по C .

Ответ №1:

Проблема в том, что когда вы определяете другие конструкторы(кроме конструктора по умолчанию) для своего класса, конструктор по умолчанию не создается компилятором автоматически.

Чтобы устранить указанную ошибку, просто добавьте любой из приведенных ниже конструкторов:

 Matrix2D() = default;  

или

 Matrix2D() {  std::coutlt;lt;"default constructor"lt;lt;std::endl; }  

в определение класса Matrix2D. Оба вышеперечисленных способа будут работать.

Ошибка 2

Ваша feedForward функция ничего не возвращает. Вы должны добавить оператор return в его тело.

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