0

I am trying to create a class Info<P,S> which holds a pointer to another object of type Info<S,P>. I have the following code.

template <class P, class S>
class Info {
….
public:
Info(…, Info<S,P>* parentInfo) : …, parentInfo(parentInfo)
{}
…
private:
Info<S, P> *parentInfo;
};

….

typedef Info<ActualP, ActualS> OneInfo;
typedef Info<ActualS, ActualP> OtherInfo;
…
OtherInfo* parentInfo =…;
OneInfo info(…, parentInfo);

This is not compiling and saying the constructor is not a valid one (at the call).

I suppose this would cause an infinite recursion while trying to resolve. Am I right? What are the alternatives to implement this intent of referencing Info<S,P> from Info<P,S>?

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
anindyaju99
  • 465
  • 1
  • 5
  • 16
  • 3
    What's the exact error? – Joseph Mansfield Apr 15 '13 at 09:46
  • Put it in another function instead of constructor. – atoMerz Apr 15 '13 at 09:49
  • @user1412084 please put the complete error message including the `candidates are` part into the original post. – scones Apr 15 '13 at 09:50
  • @AtoMerZ Thanks. Adding a set function to set it instead of the constructor works. But Info is a class instantiated at many places. Adding it to constructor would mean I won't forget to set the parent at a few places. – anindyaju99 Apr 15 '13 at 09:56
  • @user1412084 Then you should use different parameters with you construtor. I'm guessing there are two fields `S` and `P` in you `Info` class. So you should try to call the constructor like this: `Info(S, P)`. – atoMerz Apr 15 '13 at 10:00
  • add another `ctor` that *zero-initializes* `Info::parentInfo` that is used if you don't pass any parent. – Filip Roséen - refp Apr 15 '13 at 10:00

1 Answers1

1

Use typename to refer a type that depends of a template parameter:

template <class T>
class Info
{
    public:
    Info() : parentInfo(NULL) {}

    Info(Info<typename T> *info) :
        parentInfo(info)
    {
    }

    private:
        Info<typename T> *parentInfo;
};

int _tmain(int, _TCHAR**)
{
    Info<int> parent;
    Info<int> child(&parent);
}
Tio Pepe
  • 3,071
  • 1
  • 17
  • 22