templated constructor for nested classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aniait
    New Member
    • Jul 2008
    • 4

    templated constructor for nested classes

    I have declared and defined the class LinkedList as:
    Code:
    template <class T> class LinkedList {	 private:	 	class List 	{ 	public: 		List(T& aEle); 	public: 		T& iEle; 		List* iNext; 	}; 	List* iHeader;	 	List* iCurrent; public: 	LinkedList();  // Member functions };
    I am finding it difficult to define the constructor for the List class. I have something like this:
    Code:
    template <class T> LinkedList<T>::List::List(T& aEle):iEle(aEle),iNext(NULL) { } // Definition of other member functions
    The code is compiling with no errors. But, the linker is giving the following error message for the following code:
    Code:
    int main() { 	LinkedList<int> list;  	return 0; }
    linker error:
    Code:
    linkedlist.obj : error LNK2001: unresolved external symbol "public: __thiscall LinkedList<int>::List::List(int &)" (??0List@?$LinkedList@H@@QAE@AAH@Z) Debug/linkedlist.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.
    Please help me in resolving this.

    Regards,
    Anirudh
  • mkborregaard
    New Member
    • Sep 2007
    • 20

    #2
    Are you trying to divide the code into a header and a .cpp file? When implementing templated classes, the entire class should be kept in one file. Try implementing the constructor inline.

    Comment

    • aniait
      New Member
      • Jul 2008
      • 4

      #3
      Thanks. I know that making the constructor inline works. I wanted to know how to do that in a different manner.

      Regards,
      Anirudh

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Remember you are using templates. These are patterns the compiler copies and substitutes a specific type for the template type.

        That means the templates all have to be in one or more header files that are included. You cannot use a separate .cpp file as you can with actual code.

        It also means that all unbound functions used by the class template must be fully coded inside the class template. That is, inline.

        An unbound function template is a function template inside another template that does not have the type specified.

        You may have a bound function. This one has the type already specified. In this case you may a) include the template before the class template and create an instance of the template using a specific type. Then inside the class template you can use an explicit specification template function prototype.

        Comment

        • mkborregaard
          New Member
          • Sep 2007
          • 20

          #5
          OK. Why would you want to do it any other way?

          Comment

          • aniait
            New Member
            • Jul 2008
            • 4

            #6
            Well, I was wondering if I could put the definitions of all functions in a separate .inline file. That way, I could separate the declarations and definitions. Seems a bad idea now.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Yes. For templates this is won't work. For actual code, it works fine.

              Templates are more like macros and you can't have your macros using other macros in other files.

              Comment

              Working...