次のように eigen3 型を拡張したいと思います。
typedef Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> CMatrixImgParent;
class CMatrixImg : public CMatrixImgParent
{
public:
CMatrixImg() : CMatrixImgParent() {}
int Dummy(const char *filename) const {};
};
次に、eigen3 でいくつかの演算を行います。
CMatrixImg img1, img2, imgSum;
imgSum = img1 + img2;
しかし、g ++を使用するとエラーが発生するため、これは機能しません:
g++ -o bug.o -c -O2 -I/usr/include/eigen3 bug.cc
bug.cc: In function 'int main(int, char**)':
bug.cc:17:10: error: no match for 'operator=' (operand types are 'CMatrixImg' and 'const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<unsigned char>, const Eigen::Matrix<unsigned char, -1, -1, 1>, const Eigen::Matrix<unsigned char, -1, -1, 1> >')
imgSum = img1 + img2;
^
bug.cc:17:10: note: candidate is:
bug.cc:5:7: note: CMatrixImg& CMatrixImg::operator=(const CMatrixImg&)
class CMatrixImg : public CMatrixImgParent
^
bug.cc:5:7: note: no known conversion for argument 1 from 'const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<unsigned char>, const Eigen::Matrix<unsigned char, -1, -1, 1>, const Eigen::Matrix<unsigned char, -1, -1, 1> >' to 'const CMatrixImg&'
scons: *** [bug.o] Error 1
scons: building terminated because of errors.
Compilation exited abnormally with code 2 at Tue Jul 16 18:31:18
もちろん、次のような明示的なキャストによってこれを回避できます。
(*(CMatrixImgParent*)&imgSum) = img1 + img2;
しかし、これは非常に醜いです。
このタイプのキャストの必要性を回避するために、クラス定義に配置できる簡単なコードはありますか?