'C++ Expected class member or base class name [closed]

I have the following code:

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H

/**
 * @struct matrix_dims
 * @brief Matrix dimensions container. Used in MlpNetwork.h and main.cpp
 */
typedef struct matrix_dims
{
    int rows, cols;
} matrix_dims;

class Matrix
{
 public:
  // constructors and destructors
  Matrix();
  Matrix(int rows, int cols);
  Matrix(const Matrix& m); // copy-ctr
  ~Matrix();

 private:
  int _rows;
  int _cols;
  float *_mat;
};

#endif //MATRIX_H

Matrix.cpp

#include "Matrix.h"
#include <stdexcept>
#include <exception>

Matrix::Matrix (): _rows(1), _cols(1), _mat(new float[1]())
{
}

Matrix::Matrix (int rows, int cols)
{
  if (rows <= 0 || cols <= 0)
    {
      throw std::runtime_error("Invalid sizes for matrix");
    }
  _rows = rows;
  _cols = cols;
  _mat = new float[rows * cols]();
}

Matrix::Matrix (const Matrix &m):
{
  _rows = m._rows;
  _cols = m._cols;
  _mat = new float[_rows * _cols]();
  for (int i = 0; i < m._rows; i++)
    {
      for (int j = 0; j < m._cols; j++)
        {
          _mat[i * _cols + j] = m._mat[i * _cols + j];
        }
    }
}

Matrix::~Matrix ()
{
  delete[] _mat;
}

main.cpp

#include <iostream>
#include "Matrix.h"
#include "Activation.h"
#include "Dense.h"
#include "MlpNetwork.h"

Dense.h

#ifndef DENSE_H
#define DENSE_H

#include "Activation.h"

// Insert Dense class here...


#endif //DENSE_H

Activation.h

#include "Matrix.h"

#ifndef ACTIVATION_H
#define ACTIVATION_H

// Insert Activation class here...

#endif //ACTIVATION_H

MlpNetwork.h

#ifndef MLPNETWORK_H
#define MLPNETWORK_H

#include "Dense.h"

#endif // MLPNETWORK_H

however, in the implementation of the copy constructor of Matrix, I get an error by the compiler: "Expected class member or base class name". I can't see what's the problem. I guess there is a cycle dependency but I can't figure it out...

c++


Solution 1:[1]

You have an extra colon at the end of line Matrix::Matrix (const Matrix &m):. After the colon the compiler expects a class member (for example _rows) or a base class name, for example a call to another constructor.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1