2

I made a simple Vector2 Template class which I use to store an X and an Y value. Now I'm trying to keep implementation of templates in the source files, but I fail to do this with operator overloading

    class Vector2
    {
    public:
        Vector2<Type>();
        Vector2<Type>(Type x, Type y);
        Vector2<Type>(const Vector2<Type> &v);
        ~Vector2<Type>();
        Vector2<Type> operator+ (const Vector2<Type> &other)
        {
            return Vector2<Type>(x + other.x, y + other.y);
        }
    private:
        Type x, y;
    };

Now this compiles and works just fine but this is currently located in the header file. Implementing the constructor and de-constructor of Vector2 works perfectly fine aswell but when I try the following:

.h:

    Vector2<Type> operator+ (const Vector2<Type> &other);

.cpp:

    template <class Type>
    Vector2<Type>::operator+ (const Vector2<Type> &other)
    {
        return Vector2<Type>(x + other.x, y + other.y);
    }

the compiler tells me: missing type specifier - int assumed. Note C++ does not support default-int

Kind regards, Me

M4st3rM1nd
  • 207
  • 3
  • 12
  • @chris That’s not a duplicate (although this is *another* error in OP’s code). – Konrad Rudolph Apr 29 '13 at 20:13
  • @KonradRudolph, Well, with the small return type error fixed, it would be. I guess the question is more about that, even if the definition is in the cpp in the question. – chris Apr 29 '13 at 20:19

1 Answers1

4

Your definition of operator + is missing a return type:

    template <class Type>
    Vector2<Type> Vector2<Type>::operator+ (const Vector2<Type> &other)
//  ^^^^^^^^^^^^^
    {
        return Vector2<Type>(x + other.x, y + other.y);
    }

Also notice, that the definitions of a class template's member functions should appear in the same header that contains the class template definition, unless you are using explicit instantiations for all the instantiations that would otherwise be created implicitly (see this Q&A on StackOverflow).

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Thanks for the answer. Can't believe I was so blind. But I get a linker error now. I used Method one from this article [How to split template](http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp) but it didn't prevent the linker error. Or do you think implementing a template in a header isn't bad practice? – M4st3rM1nd Apr 29 '13 at 20:13
  • @M4st3rM1nd: That method would not work IMO. It would only generate the code for the constructor and destructor, not for all other member functions. Explicit instantiation is the way to go (see the Q&A linked in the answer) – Andy Prowl Apr 29 '13 at 20:16
  • 1
    @M4st3rM1nd: Putting definitions of member functions of a class template in headers is not bad practice, actually it is common practice. Of course this isn't ideal - one would like to hide implementation in a cpp file sometimes - but today's C++ does not allow doing otherwise, unless you go the explicit instantiation way – Andy Prowl Apr 29 '13 at 20:17