Zebrafish
Zebrafish

Reputation: 13886

How do I declare this templated class function as a friend?

Templates are making things so hard. I haven't got the syntax right, I've been trying for ages. I have a function called "createSphere" in a class called "ModelMngr" that I would like to be a friend of another class.

class ModelMngr
{public:
template <typename T_scalar>
    static Sphere<T_scalar>* createSphere(T_scalar radius, int sides, int heightSegments, const String& name = "Sphere Primitive Mesh");
}

template <typename T_scalar>
class Sphere : public MeshBaseClass<T_scalar>
{public:
        template <typename T> friend Sphere<T>* ModelMngr::createSphere(T radius, int sides, int heightSegments, const String& name = "Sphere Primitive Mesh");


}

I changed the T_scalar in the friend declaration to T because I was worried there would be a name conflict with the template argument of the enclosing class. Is there any conflict?

Upvotes: 0

Views: 64

Answers (1)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29965

Forward declare Sphere, remove the default arguments from friend declaration and add semicolons after class definitions:

template <typename T_scalar>
class Sphere;

class ModelMngr {
public:
  template <typename T_scalar>
  static Sphere<T_scalar> *
  createSphere(T_scalar radius, int sides, int heightSegments,
               const String& name = "Sphere Primitive Mesh");
};

template <typename T_scalar>
class Sphere : public MeshBaseClass<T_scalar> {
public:
  template <typename T>
  friend Sphere<T> *
  ModelMngr::createSphere(T radius, int sides, int heightSegments,
                          const String &name);
};

Upvotes: 3

Related Questions