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

Viewed 46

I have a template class alpha_x give as,

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_) {};
};

I have another class with an overload for operator =

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

When we try to define this function,

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

Now this gives an error "Too many template parameters in template redeclaration". How do I sort this out?

1 Answers

T template parameter is a class Scalar's template parameter. Thus it needs to be specified in a separate template parameter list.

Following would work:

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