【问题标题】:can you give a derived class object as a function parameter in place of a base class object您可以将派生类对象作为函数参数代替基类对象吗
【发布时间】:2018-08-04 18:15:07
【问题描述】:

我正在尝试使用表达式模板进行矩阵加法,对于这个任务我有基类:Exp

template<typename subtype>
class Exp{
public:
  inline const subtype& self(void) const{
    return *static_cast<const subtype*>(this);
  }

};

派生类:矩阵

template<typename T,unsigned rows_,unsigned cols_ >
class matrix : public Exp<matrix<T,rows_,cols_>>{
   //some members
};

还有另一个派生类:addExp

template<typename T, typename Op1 , typename Op2>
class addExp: public Exp< addExp<T,Op1,Op2> >{
  const Op1& op1;
  const Op2& op2;

public:
  addExp(const Op1& a, const Op2& b): op1(a), op2(b){}

  T& operator()(const std::size_t i,const std::size_t j) const{ 
    return op1(i,j) + op2(i,j); 
  }
}; 

我现在正在尝试在 addExp 上进行运算符重载以添加矩阵。

template<typename T,typename Lhs, typename Rhs>
inline addExp<T,Lhs, Rhs>
operator+(const Exp<Lhs> &lhs, const Exp<Rhs> &rhs) {
  return addExp<T,Lhs, Rhs>(lhs.self(), rhs.self());
}

稍后在我的代码中,我尝试将两个矩阵对象(应该以 Exp 作为基类)作为函数参数,但出现错误:

prog.cpp: In function ‘int main()’:
prog.cpp:76:25: error: no match for ‘operator+’ (operand types are ‘matrix<int, 3u, 3u>’ and ‘matrix<int, 3u, 3u>’)
  matrix<int,3,3> m3 = m1+m2;
                       ~~^~~
prog.cpp:69:1: note: candidate: template<class T, class Lhs, class Rhs> addExp<T, Lhs, Rhs> operator+(const Exp<Lhs>&, const Exp<Rhs>&)
 operator+(const Exp<Lhs> &lhs, const Exp<Rhs> &rhs) {
 ^~~~~~~~
prog.cpp:69:1: note:   template argument deduction/substitution failed:
prog.cpp:76:26: note:   couldn't deduce template parameter ‘T’
  matrix<int,3,3> m3 = m1+m2;  

我在哪里出错了,我该如何解决?

【问题讨论】:

  • T 与您传入的参数没有直接关系,因此编译器无法推断出它应该是什么。

标签: c++ c++11 c++14 template-meta-programming


【解决方案1】:

您需要以某种方式将T 的信息传递给您的operator+。它可以通过多种方式完成,哪种方式最好在很大程度上取决于您的用例。

这是一种方法。

template<typename T,unsigned rows_,unsigned cols_ >
class matrix : public Exp<matrix<T,rows_,cols_>>{
    using type = T;
    //some members
};

template<typename Lhs, typename Rhs, typename T = typename Lhs::type>
inline addExp<T,Lhs, Rhs>
operator+(const Exp<Lhs> &lhs, const Exp<Rhs> &rhs) {
  return addExp<T,Lhs, Rhs>(lhs.self(), rhs.self());
}

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    相关资源
    最近更新 更多