Templates

Started by
6 comments, last by jamesleighe 12 years, 5 months ago
Best explained in code:

I have this.

template<class T>
class OcTreeOptimized
{
public:
template<class T>
class Node
{
Node ();
}
};


But I can't do this!

template<class T>
OcTreeOptimized<T>::Node<T>::Node ()
{
; // do stuff
}


I get the compiler error "error C3860: template argument list following class template name must list parameters in the order used in template parameter list".
Is there any way around this?

Thanks as usual!
Advertisement
You can do that like:

template<class T>
class OcTreeOptimized {
public:
template<class U>
class Node {
Node ();
};
};

template <class T>
template <class U>
OcTreeOptimized<T>::Node<U>::Node() {
// stuff
}

Best explained in code:

I have this.

template<class T>
class OcTreeOptimized
{
public:
template<class T>
class Node
{
Node ();
}
};


But I can't do this!

template<class T>
OcTreeOptimized<T>::Node<T>::Node ()
{
; // do stuff
}


I get the compiler error "error C3860: template argument list following class template name must list parameters in the order used in template parameter list".
Is there any way around this?

Thanks as usual!



If you want Node to be of type T, then upon creating OcTreeOptimized, the type of T will be known, thus you can simply do this :

template<typename T>
class Foo{
class Bar{
T val;
Bar() : {}
}
};


else you can take SiCrane's suggestion
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
I can't think of a good reason for templating the inner Node class on a *different* type to the outer Octree class. Do you intend for them to be templated on the same type T?

If that is not what you meant, then go with SiCrane's solution, and name the template types two different names.

But if you want them templated on the same type, then you don't need a template declaration at all on the inner class - inner classes of template classes are already templated based on the same template as the surrounding class.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


I can't think of a good reason for templating the inner Node class on a *different* type to the outer Octree class. Do you intend for them to be templated on the same type T?

If that is not what you meant, then go with SiCrane's solution, and name the template types two different names.

But if you want them templated on the same type, then you don't need a template declaration at all on the inner class - inner classes of template classes are already templated based on the same template as the surrounding class.


This is what I meant, I see what I did wrong now.
Follow up question!

This code:


template <class T> void
OcTreeOptimized<T>::FindNodesIntersectingAABox (Vector3D& mins, Vector3D& maxs, Vector<OcTreeOptimized::Node*>& nodes)
{
;
}


Generates "warning C4346: 'OcTreeOptimized<T>::Node' : dependent name is not a type prefix with 'typename' to indicate a type".
It then generates a bunch of syntax errors.

Whats going on here?


(Here is the new declaration in case)


template<class T>
class OcTreeOptimized
{
public:
class Node
{
;
};
};


Thanks!

This code:

template <class T> void
OcTreeOptimized<T>::FindNodesIntersectingAABox (Vector3D& mins, Vector3D& maxs, Vector<OcTreeOptimized::Node*>& nodes)
{
;
}

Generates "warning C4346: 'OcTreeOptimized<T>::Node' : dependent name is not a type prefix with 'typename' to indicate a type".


You are using a dependent name (OcTreeOptimized::Node) to refer to a type, and according to the standard the compiler isn't allowed to interpret dependent names as types without an explicit 'typename' declaration. In theory at least, this ought to solve it:

template <class T> void
OcTreeOptimized<T>::FindNodesIntersectingAABox (Vector3D& mins, Vector3D& maxs, Vector<typename OcTreeOptimized::Node *>& nodes)
{
}

Note that the first response from google for that "warning C4346" answers your question in some detail.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I should have probably googled it first since it's such a specific error that's true.

This topic is closed to new replies.

Advertisement