[英]C++ Trouble declaring function prototype within header file
I have been given the following code to work with within a MatrixTest.cpp function: 我已经获得以下代码在MatrixTest.cpp函数中使用:
Matrix matrix = Matrix::Zeros(2,4)
The aim is to "create a 2x4 matrix of zeros with the static Zeros" and I need to be able to add something to the header file "Matrix.h" which allows "MatrixTest.cpp" to compile for the line of code above. 目的是“用静态零创建一个2x4零的矩阵”,我需要能够在头文件“Matrix.h”中添加一些东西,它允许“MatrixTest.cpp”编译上面的代码行。 This is the code in my header file so far: 到目前为止,这是我的头文件中的代码:
#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_ */
This gives the error in my .cpp file that I cannot call member function Matrix Matrix::Zeros(int, int) without object. 这给出了我的.cpp文件中的错误,我无法在没有对象的情况下调用成员函数Matrix Matrix :: Zeros(int,int)。 But surely Zeros is my object and my Matrix class is my type? 但是肯定Zeros是我的对象而我的Matrix类是我的类型?
If I change my code in the header file to the following: 如果我将头文件中的代码更改为以下内容:
static Zeros(const int noOfRows, const int noOfCols);
Then I get an error within my .h file saying "forbids declaration of 'Zeros' with no type and an error within my .cpp file saying "conversion from 'int' to non-scalar type 'Matrix' requested" 然后我在我的.h文件中得到一个错误,说“禁止声明'Zeros'没有类型和我的.cpp文件中的错误说”从'int'转换为非标量类型'Matrix'请求“
I'm confused since I would've thought that my type is Matrix as it appears underneath the class Matrix and that since Matrix::Zeros(2,4) follows the constructor Matrix(const int noOfRows, const int noOfCols) then there wouldn't be a conversion issue from 'int' to non-scalar type. 我很困惑,因为我认为我的类型是Matrix,因为它出现在Matrix类下面,并且因为Matrix :: Zeros(2,4)遵循构造函数Matrix(const int noOfRows,const int noOfCols)然后就不会不是从'int'到非标量类型的转换问题。
Can anybody help with this as I seem to be going back and forth between these errors? 任何人都可以帮忙解决这个问题,因为我似乎在这些错误之间来回走动?
The signature of the function should be 函数的签名应该是
static Matrix Zeros(const int noOfRows, const int noOfCols);
The static
keyword is not the return type, Matrix
is. static
关键字不是返回类型, Matrix
是。 Rather the static
keyword states that you do not need an instance of a Matrix
to call the method, instead you can call it as 相反, static
关键字表明您不需要Matrix
的实例来调用该方法,而是可以将其称为
Matrix matrix = Matrix::Zeros(2,4)
To be clear, if you did not use the word static
then you would have to do something like 需要明确的是,如果你没有使用这个词static
,那么你就必须做一些像
Matrix a{};
Matrix matrix = a.Zeros(2,4);
but you can see that the Zeros
method doesn't depend on the state of a
so it would make sense for the method to be static
instead. 但可以看到的是, Zeros
方法不依赖于国家a
所以它会是有意义的方法是static
,而不是。
Since static
is not a return type here, and your function is returning a Matrix
, that would be your return type. 由于static
不是返回类型,并且您的函数返回Matrix
,这将是您的返回类型。
Changing your function signature to static Matrix Zeros(const int noOfRows, const int noOfCols);
将函数签名更改为static Matrix Zeros(const int noOfRows, const int noOfCols);
should do the trick. 应该做的伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.