C++ 无法在头文件中声明函数原型
C++ Trouble declaring function prototype within header file
我得到了以下代码,可以在 MatrixTest.cpp 函数中使用:
Matrix matrix = Matrix::Zeros(2,4)
目标是 "create a 2x4 matrix of zeros with the static Zeros",我需要能够向头文件 "Matrix.h" 添加一些内容,这允许 "MatrixTest.cpp" 编译上面的代码行。到目前为止,这是我的头文件中的代码:
#ifndef MATRIX_H_
#define MATRIX_H_
class Matrix {
protected:
// These are the only member variables allowed!
int noOfRows;
int noOfColumns;
double *data;
int GetIndex (const int rowIdx, const int columnIdx) const;
public:
Matrix (const int noOfRows, const int noOfCols);
Matrix (const Matrix& input);
Matrix& operator= (const Matrix& rhs);
~Matrix ();
Matrix Zeros(const int noOfRows, const int noOfCols);
};
#endif /* MATRIX_H_ */
这在我的 .cpp 文件中给出了错误,我无法在没有对象的情况下调用成员函数 Matrix Matrix::Zeros(int, int)。但是肯定Zeros是我的对象而我的Matrixclass是我的类型?
如果我将头文件中的代码更改为以下内容:
static Zeros(const int noOfRows, const int noOfCols);
然后我在我的 .h 文件中收到一个错误,说 "forbids declaration of 'Zeros' with no type and an error within my .cpp file saying " 请求从 'int' 转换为非标量类型 'Matrix'"
我很困惑,因为我认为我的类型是 Matrix,因为它出现在 class Matrix 下面,而且因为 Matrix::Zeros(2,4) 遵循构造函数 Matrix(const int noOfRows, const int noOfCols) 那么就不会有从 'int' 到非标量类型的转换问题。
任何人都可以帮助解决这个问题,因为我似乎在这些错误之间来回走动吗?
函数的签名应该是
static Matrix Zeros(const int noOfRows, const int noOfCols);
static
关键字是不是return类型,Matrix
是。相反,static
关键字声明您不需要 Matrix
的实例来调用该方法,而是可以将其称为
Matrix matrix = Matrix::Zeros(2,4)
明确地说,如果您没有使用static
这个词,那么您将不得不做类似
的事情
Matrix a{};
Matrix matrix = a.Zeros(2,4);
但是您可以看到 Zeros
方法不依赖于 a
的状态,因此将方法改为 static
是有意义的。
由于 static
不是此处的 return 类型,而您的函数是 returning 一个 Matrix
,那将是您的 return 类型。
将您的函数签名更改为
static Matrix Zeros(const int noOfRows, const int noOfCols);
应该可以解决问题。
我得到了以下代码,可以在 MatrixTest.cpp 函数中使用:
Matrix matrix = Matrix::Zeros(2,4)
目标是 "create a 2x4 matrix of zeros with the static Zeros",我需要能够向头文件 "Matrix.h" 添加一些内容,这允许 "MatrixTest.cpp" 编译上面的代码行。到目前为止,这是我的头文件中的代码:
#ifndef MATRIX_H_
#define MATRIX_H_
class Matrix {
protected:
// These are the only member variables allowed!
int noOfRows;
int noOfColumns;
double *data;
int GetIndex (const int rowIdx, const int columnIdx) const;
public:
Matrix (const int noOfRows, const int noOfCols);
Matrix (const Matrix& input);
Matrix& operator= (const Matrix& rhs);
~Matrix ();
Matrix Zeros(const int noOfRows, const int noOfCols);
};
#endif /* MATRIX_H_ */
这在我的 .cpp 文件中给出了错误,我无法在没有对象的情况下调用成员函数 Matrix Matrix::Zeros(int, int)。但是肯定Zeros是我的对象而我的Matrixclass是我的类型?
如果我将头文件中的代码更改为以下内容:
static Zeros(const int noOfRows, const int noOfCols);
然后我在我的 .h 文件中收到一个错误,说 "forbids declaration of 'Zeros' with no type and an error within my .cpp file saying " 请求从 'int' 转换为非标量类型 'Matrix'"
我很困惑,因为我认为我的类型是 Matrix,因为它出现在 class Matrix 下面,而且因为 Matrix::Zeros(2,4) 遵循构造函数 Matrix(const int noOfRows, const int noOfCols) 那么就不会有从 'int' 到非标量类型的转换问题。
任何人都可以帮助解决这个问题,因为我似乎在这些错误之间来回走动吗?
函数的签名应该是
static Matrix Zeros(const int noOfRows, const int noOfCols);
static
关键字是不是return类型,Matrix
是。相反,static
关键字声明您不需要 Matrix
的实例来调用该方法,而是可以将其称为
Matrix matrix = Matrix::Zeros(2,4)
明确地说,如果您没有使用static
这个词,那么您将不得不做类似
Matrix a{};
Matrix matrix = a.Zeros(2,4);
但是您可以看到 Zeros
方法不依赖于 a
的状态,因此将方法改为 static
是有意义的。
由于 static
不是此处的 return 类型,而您的函数是 returning 一个 Matrix
,那将是您的 return 类型。
将您的函数签名更改为
static Matrix Zeros(const int noOfRows, const int noOfCols);
应该可以解决问题。