【问题标题】:Passing object of a Templated struct to a member function of another templated class将模板结构的对象传递给另一个模板类的成员函数
【发布时间】:2020-04-01 20:06:40
【问题描述】:

我有一个模板类 alpha_x 作为,

template <typename T,typename U>
struct alpha_x {
    const T & alpha;
    const Scalar<U> & x;
    alpha_x(const T & a_, const Scalar<U> & x_) : alpha(a_), x(x_) {};
};

我有另一个类,运算符重载 =

template <typename T>
class Scalar{
    ...
    template <typename U,typename V>
    const Scalar<T> & operator = (alpha_x<U,V> c);
    ...
}

当我们尝试定义这个函数时,

template <typename T,typename U,typename V>
const Scalar<T> & Scalar<T>::operator = (alpha_x<U,V> c){
    //do something...
}

现在这会给出错误“模板重新声明中的模板参数过多”。我该如何解决这个问题?

【问题讨论】:

  • 你试过template &lt;typename T&gt; template&lt;typename U,typename V&gt;吗?
  • 这解决了...谢谢

标签: c++ templates


【解决方案1】:

T模板参数是类Scalar的模板参数。因此需要在单独的模板参数列表中指定。

以下方法可行:

template <typename T>
template <typename U, typename V>
const Scalar<T> & Scalar<T>::operator = (alpha_x<U,V> c){
    // do something...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多