I came across a head scratcher in my coding endeavors, and was wondering if anyone could give me some insight on it. I have a header only class, that I was going to split into .h/.cpp, and while writing out the .cpp file, I noticed that I had no idea how to write methods for a nested class in a class template. Here's a code snippet (small part of what already runs):
namespace core {
namespace container {
template <class T>
class List { // the class
protected:
class Node {
public:
T data;
Node* next;
inline Node(T d) : data(d),next(NULL) {}
inline Node(T d,Node* n) : data(d),next(n) {}
inline ~Node() {next = NULL;}
};
public:
// the rest of the class template, a here
};
};
};
The snippet is highly stripped down, with relevant stuff left behind. Normally, I don't deal with this as I tend to keep all my template stuff header-only, but compiling needs in other parts of the project would be smoother if I compile all this and hang on to the objects.
So initially I wrote
using namespace core::container;
template <class T>
List<T>::Node::Node(T d) {}
Now this isn't exactly the line, because I honestly don't intend on removing the inline constructor anyway, but I used it as an example because it's at the top of the code and simple.
I'm not sure if it was me or just that my IDE red-lined it, but it looked wrong to me. I'm away from my compiling machine so I can't run a test on it, and was wondering if I could get some insight before I write a few hundred incorrect lines.
So, am I doing this wrong? What is the proper way of defining a member function (or constructor in this example) for a subclass of a class template?