JiiPee <
n...@notvalid.com> wrote in news:DYcww.293940$Ud7.2...@fx11.am4:
As I said before, you need to tell the computer the friend operator* is
not from the closest scope (class Vector2D2), but from another scope.
Unfortunately, just adding "::" for global scope does not work as this
gets merged to the preceding token (Vector2D2<T>). One solution might be
to put the operator* in a namespace (all custom C++ code ought to be in a
namespace anyway, so this should not a problem). The following at least
compiles with g++ (but crashes the MSVC++ compiler ;-): Probably there
are other cleaner solutions...
#include <iostream>
using namespace std;
namespace abc {
template <typename T> class Vector2D2;
template <typename T> Vector2D2<T> operator * (const T& pointXY, const
Vector2D2<T>& point);
template <typename T>
class Vector2D2
{
public:
// if I commend this out the code compiles with no problems
Vector2D2 operator * (const Vector2D2& point) const { return *this; }
friend Vector2D2<T> abc::operator*<>(const T& pointXY, const
Vector2D2<T>& point);
int y;
private:
T b;
};
template <typename T>
Vector2D2<T> operator* (const T& pointXY, const Vector2D2<T>& point)
{
Vector2D2<T> cc;
cc.b = point.b * pointXY;
return cc;
}
} // namespace abc
using namespace abc;