Matrix的编写接口
Writing interface for Matrix
我正在用 C++ 编写 Matrix class 的接口
所以所有函数都需要是虚拟的
现在在下面的代码中我定义了一个虚函数 virtual Matrix add(Matrix A, Matrix B) const = 0;
我看到的问题是 class 矩阵未定义。所以我很困惑我应该定义
class界面中的矩阵?或者有没有更好的接口实现方式
class MatrixInterface
{
public:
virtual Matrix add(Matrix A, Matrix B) const = 0;
};
出现编译错误的更新代码:
在 Matrix.cpp 中添加方法:member function declared with 'override' does not override a base class memberC/C++(1455)
#include <iostream>
#include "MatrixInterface.h"
class Matrix : public MatrixInterface
{
public:
Matrix *add(MatrixInterface *A, MatrixInterface *B) override{
// implementation
};
};
class MatrixInterface
{
public:
virtual MatrixInterface *add(MatrixInterface *A, MatrixInterface *B) const = 0;
};
Return接口类型来自add
方法在你的接口class,然后在subclass,你可以覆盖虚拟方法并改变return 输入 Matrix
(或任何 narrower type)。
class MatrixInterface
{
public:
virtual MatrixInterface* add(MatrixInterface* A, MatrixInterface* B) const = 0;
};
派生 class:
class Matrix : public MatrixInterface
{
public:
Matrix* add(MatrixInterface* A, MatrixInterface* B) override
{
// implementation
};
};
您将需要使用指针来利用 covariant return types。
我正在用 C++ 编写 Matrix class 的接口
所以所有函数都需要是虚拟的
现在在下面的代码中我定义了一个虚函数 virtual Matrix add(Matrix A, Matrix B) const = 0;
我看到的问题是 class 矩阵未定义。所以我很困惑我应该定义
class界面中的矩阵?或者有没有更好的接口实现方式
class MatrixInterface
{
public:
virtual Matrix add(Matrix A, Matrix B) const = 0;
};
出现编译错误的更新代码:
在 Matrix.cpp 中添加方法:member function declared with 'override' does not override a base class memberC/C++(1455)
#include <iostream>
#include "MatrixInterface.h"
class Matrix : public MatrixInterface
{
public:
Matrix *add(MatrixInterface *A, MatrixInterface *B) override{
// implementation
};
};
class MatrixInterface
{
public:
virtual MatrixInterface *add(MatrixInterface *A, MatrixInterface *B) const = 0;
};
Return接口类型来自add
方法在你的接口class,然后在subclass,你可以覆盖虚拟方法并改变return 输入 Matrix
(或任何 narrower type)。
class MatrixInterface
{
public:
virtual MatrixInterface* add(MatrixInterface* A, MatrixInterface* B) const = 0;
};
派生 class:
class Matrix : public MatrixInterface
{
public:
Matrix* add(MatrixInterface* A, MatrixInterface* B) override
{
// implementation
};
};
您将需要使用指针来利用 covariant return types。