两个具有转换方法的类
Two classes with conversion methods
我在C++中有两个矩阵类,它们继承自同一个基类MatrixType
。它们使用不同的方法来存储稀疏矩阵,并且是类模板,因为它们的条目可能属于不同的类型。
每个矩阵类型都应该有一个允许转换为另一种类型的方法。问题是,如果我在 MatrixCOO
类中声明toCRS
,则参数的MatricCRS
类型仍未定义。如何解决这个问题?
class MatrixType { /*...*/ };
template<typename Scalar>
class MatrixCOO {
// Private stuff...
public:
// Public stuff...
void toCRS(MatrixCRS & target) { // Issue is here: MatrixCRS is undefined
// Fill target with elements from *this
}
}
template<typename Scalar>
class MatrixCRS {
// Private stuff...
public:
// Public stuff...
void toCOO(MatrixCOO & target) {
// Fill target with elements from *this
}
}
PS:在我的理解中,即使我在MatrixCOO
之前声明类MatrixCRS
,在声明MatrixCRS::toCOO (MatrixCOO &)
时仍然会遇到同样的问题。
向前声明一个,声明两个,定义需要两个类定义的函数:
class MatrixType { /*...*/ };
template<typename Scalar> class MatrixCRS; // Forward declaration
template<typename Scalar>
class MatrixCOO {
// Private stuff...
public:
// Public stuff...
void toCRS(MatrixCRS<Scalar>& target); // Just declare the method
};
template<typename Scalar>
class MatrixCRS {
// Private stuff...
public:
// Public stuff...
void toCOO(MatrixCOO<Scalar>& target) {
// ...
}
};
// Implementation, we have both class definitions
template<typename Scalar>
void MatrixCOO<Scalar>::toCRS(MatrixCRS<Scalar> & target)
{
// ...
}
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 比较两个节点坐标的最佳方法是什么?
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 检查两个向量是否并行的最有效方法
- 用相同的参数声明两个构造函数的最偶像化的方法是什么?
- 将两个uint8_t转换为int16_t的正确现代方法
- 为什么转换涉及两个用户定义的转换功能/构造函数
- 如何将此代码转换为两个以上的数字(计算HCF)
- 将浮点类型转换为两个短字节(高字节和低字节)
- 两个具有转换方法的类
- 将指针 (T*) 强制转换或转换为两个常量 (T 常量 * 常量) 指针
- 返回类型 std::bind 隐式转换为两个不同的显式构造函数
- c++中最快的从成对向量转换为两个独立向量的方法
- 在c/++中将浮点数转换为两个int
- 将两个字节转换为16位值的最有效方法
- 将一个 UNICODE 字符转换为两个 ASCII 字符