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
错误信息如下:
main.cpp|119|error: no matching function for call to 'Matrix2D::Matrix2D()'|
我的猜测是,当在 class NeuralNewtork
中声明 Matrix2D first_hidden_weights
时,它需要 2 个参数,因为 class Matrix2D
-s 构造函数。但是我不能给它参数,因为我还不知道它们。
#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){
}
};
问题 是当您为 class 定义其他构造函数(默认构造函数除外)时,编译器不会自动生成默认构造函数。
要解决提到的错误,只需添加下面显示的构造函数之一:
Matrix2D() = default;
或
Matrix2D()
{
std::cout<<"default constructor"<<std::endl;
}
进入您的 Matrix2D class 定义。以上两个都可以。
错误2
您的 feedForward
函数没有 return 任何东西。您应该在其正文中添加一个 return 语句。
Matrix2D feedForward(Matrix2D input)
{
return Matrix2D(); //added this return
}
错误信息如下:
main.cpp|119|error: no matching function for call to 'Matrix2D::Matrix2D()'|
我的猜测是,当在 class NeuralNewtork
中声明 Matrix2D first_hidden_weights
时,它需要 2 个参数,因为 class Matrix2D
-s 构造函数。但是我不能给它参数,因为我还不知道它们。
#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){
}
};
问题 是当您为 class 定义其他构造函数(默认构造函数除外)时,编译器不会自动生成默认构造函数。
要解决提到的错误,只需添加下面显示的构造函数之一:
Matrix2D() = default;
或
Matrix2D()
{
std::cout<<"default constructor"<<std::endl;
}
进入您的 Matrix2D class 定义。以上两个都可以。
错误2
您的 feedForward
函数没有 return 任何东西。您应该在其正文中添加一个 return 语句。
Matrix2D feedForward(Matrix2D input)
{
return Matrix2D(); //added this return
}