template inside a template class

Hello guys :)

I have a templatised class, and inside it I want to define a templatised operator. But the problem is that I'm succeeding with this only if I combine the prototype and the implementation. Could you guys tell me how to make it in 2 parts?

1
2
3
4
5
6
7
8
9
10
11
template <typename T>
class myClass
{
    T x;
public:
    template <typename U>
    myClass<T>& operator=(const vector<U> src)
    {
        x = src;
    }
};


So I'm looking for something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
class myClass
{
    T x;
public:
    template <typename U>
    myClass<T>& operator=(const vector<U> src);
};
template <typename U>
template <typename T>
myClass<T>& operator=(const vector<U> src)
{
    x = src;
}


but this last one doesn't compile.

Thanks for any efforts :)
Last edited on
You can do template<typename T, typename U> for the second and it should work fine.
Last edited on
Thank you for your answer.

Yours doesn't work, pal! it says that the function doesn't match the on in the class!

I had to do it just the way I wrote it, but the other way around! so something like:

1
2
3
template <typename T>
template <typename U>
myClass<T>& operator=(const vector<U> src)


Could you please show me an exmaple where your example could work?
LB's answer won't work in any context; the way you did is the only way.
Thank you all :)
Topic archived. No new replies allowed.