簡體   English   中英

將模板結構的對象傳遞給另一個模板類的成員函數

[英]Passing object of a Templated struct to a member function of another templated class

我有一個模板類 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...
}

現在這給出了一個錯誤“模板重新聲明中的模板參數太多”。 我該如何解決這個問題?

T模板參數是一個Scalar類的模板參數。 因此它需要在單獨的模板參數列表中指定。

以下將起作用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM