在辅助类中访问boost矩阵的子片段

时间:2010-06-19 00:54:58

标签: c++ boost matrix

我正在尝试构建一个简化boost boost :: numeric :: ublas :: matrix使用的类。因此,我得到了:

using namespace boost::numeric::ublas;
typedef matrix<double> matrix_t;

class Tensor : public matrix_t {

  public:
    Tensor (const int M, const int N) : matrix_t(M, N) { } 
    virtual ~Tensor() { } 

    Tensor SubMatrix (const int start1, const int size1, const int start2, const int size2) const;

    void Print() const;

}; // tensor

我进一步定义SubMatrix()如下:

Tensor Tensor::SubMatrix (const int start1, const int size1, const int start2, const int size2) const {

  matrix_slice<matrix_t> s (this, slice(start1, 1, size1), slice(start2, 1, size2));
  Tensor t (matrix_expression<matrix_t> (s));

  return t;
}  

我希望能够通过从Tensor中抓取子矩阵轻松创建新的Tensor。编译器抱怨如下:

g++ -ftemplate-depth-100 -Drestrict= -Wall -Wno-deprecated -g3 -ggdb -Wall -D_GLIBCXX_DEBUG  -I/home/eshamay/md/src -I/home/eshamay/share/include -I/usr/include -I/usr/local/include -I/home/eshamay/src/boost-1_43_0  -L/home/eshamay/share/lib -L/home/eshamay/src/lapack-3.2.1 -lconfig++  -c -o morita2002.o morita2002.cpp
morita2002.cpp: In member function ‘morita::math::Tensor morita::math::Tensor::SubMatrix(int, int, int, int) const’:
morita2002.cpp:109: error: no matching function for call to ‘boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::          basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const morita::math::Tensor* const,  boost::numeric::ublas::slice, boost::numeric::ublas::slice)’
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3192: note: candidates are: boost::numeric::ublas::matrix_slice<E>::matrix_slice(const typename boost::mpl::          if_<boost::is_const<T>, typename M::const_closure_type, typename M::closure_type>::type&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::      difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&, int) [with M = boost::numeric::ublas::matrix<double,      boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >]
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3183: note:                 boost::numeric::ublas::matrix_slice<E>::matrix_slice(M&, const boost::numeric::ublas::    basic_slice<typename A::size_type, typename A::difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&) [with M =  boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::             allocator<double> > >]
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3155: note:                 boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >&)
morita2002.cpp:112: error: conversion from ‘morita::math::Tensor (*)(boost::numeric::ublas::matrix_expression<boost::numeric::ublas::matrix<double, boost::numeric::    ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >)’ to non-scalar type ‘morita::math::       Tensor’ requested

在尝试了许多变体来安抚编译器之后,我很难过。问题是什么,为什么我不能像编写它一样创建matrix_slice?

1 个答案:

答案 0 :(得分:1)

matrix_slice需要一个引用作为它的第一个参数,而你给它一个指针(this)。请尝试使用*this

此外,由于您的SubMatrix方法具有const限定符,因此*的类型为matrix_t const&,因此您应使用matrix_slice<matrix_t const>