Reputation: 3546
I have two matrix classes in C++ which inherit from the same base class MatrixType
. They use different methods for storing sparse matrices, and are class templates because their entries may be of different types.
Each matrix type should have a method to allow to convert to the other type. The issue is that if I declare toCRS
in the MatrixCOO
class, the MatricCRS
type for the argument is still undefined. How can I solve this problem?
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: in my understanding, even if I were to declare the class MatrixCRS
before MatrixCOO
, I would still be faced with the same problem in declaring MatrixCRS::toCOO (MatrixCOO &)
.
Upvotes: 0
Views: 42
Reputation: 217293
Forward declare one, declare both, define function requiring both class definitions:
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)
{
// ...
}
Upvotes: 2