0

I am defining a Vec2 class with a friend functions. I am getting the error: argument list for class template Vec2 is missing for the friend function: friend Vec2 operator * (const T &r, const Vec2 &v).

    template<typename T>
    class Vec2
    {
    public:
        Vec2() : x(0), y(0) {}
        Vec2(T xx) : x(xx), y(xx) {}
        Vec2(T xx, T yy) : x(xx), y(yy) {}
        Vec2 operator + (const Vec2 &v) const
        { return Vec2(x + v.x, y + v.y); }
        Vec2 operator / (const T &r) const
        { return Vec2(x / r, y / r); }
        Vec2 operator * (const T &r) const
        { return Vec2(x * r, y * r); }
        Vec2& operator /= (const T &r)
        { x /= r, y /= r; return *this; }
        Vec2& operator *= (const T &r)
        { x *= r, y *= r; return *this; }
        friend std::ostream& operator << (std::ostream &s, const Vec2<T> &v)
        {
            return s << '[' << v.x << ' ' << v.y << ']';
        }
        friend Vec2 operator * (const T &r, const Vec2<T> &v)
        {   return Vec2(v.x * r, v.y * r); }
        T x, y;
    };
  • your code compiles fine with gcc 5. Which compiler are you using? – ISanych Aug 20 '15 at 14:22
  • I cannot reproduce your problem (code compiles fine with g++ 4.9.2 and -Wall). Even if I try `Vec2 v2; 1.0*v2;`. Could you provide a `main()` that triggers the error? – ex-bart Aug 20 '15 at 14:24
  • 1
    [OT] You should implement your free and member `operator*` in terms of your `operator*=` to avoid unnecessary code duplication. – Barry Aug 20 '15 at 14:24
  • I tried it with VS 2010 and it fails. Not sure if I need to set up some flags in the compiler – Francisco Porrata Aug 20 '15 at 14:47
  • It is possible VS 2010 is more picky when it comes to uses of the injected class name. Try replacing plain `Vec2` by `Vec2` in the declaration and the definition of `operator*`. – ex-bart Aug 20 '15 at 14:54
  • Found issue VS2010 does not support all the options in c++11. Used g++ and worked fine. Thanks for your help. – Francisco Porrata Aug 20 '15 at 15:45
  • For future searchers: [Support Matrix For C++11/14/17 Features in Visual Studio](https://msdn.microsoft.com/en-CA/library/hh567368.aspx) – user4581301 Aug 20 '15 at 15:49

0 Answers0