James B
James B

Reputation: 9595

C++ Using the operator keyword with a template?

I'm stuggling with defining friend operator functions. My code is as follows:

    template <typename typ>
    class VecClass 
    {
     public:
        VecClass();
        /* other class definitions */
        friend void operator+(VecClass op1,VecClass op2);
    }

    template <typename typ>
    void VecClass<typ>::operator+(VecClass<typ> &op1,VecClass<typ> &op2)
    {
        /* do some stuff on op1 and op2 in here */
    }

where VecClass is a class to create vectors and perform various functions on those vectors (N.B. I've simplified the code to try and be as clear as possible). When compiling, using

    int main()
    {
        VecClass=a,b;
        a+b;
        return 0;
    }

I get the following compilation error

     error C2039: '+' : is not a member of 'VecClass<typ>'

I'm clearly missing something and would be grateful for any suggestions. Thanx.

Upvotes: 2

Views: 183

Answers (1)

inkooboo
inkooboo

Reputation: 2944

You declared a friend operator, not class member, so remove VecClass<typ>::

template <typename typ>
void operator+(VecClass<typ> &op1,VecClass<typ> &op2)
{
        /* do some stuff on op1 and op2 in here */
}

Upvotes: 6

Related Questions