Trouble with Template classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raistlinx
    New Member
    • Jul 2007
    • 14

    Trouble with Template classes

    Hi. I'm trying to write series of classes using templates and am running into what must be a simple error. I'm a bit of a noob so I apologize in advance if this kind of question is not welcome here.

    When I try to run the program below i get the following error during build (using Visual Studio).

    Data Structures error LNK2019: unresolved external symbol "public: __thiscall DLNode<int>::DL Node<int>(int)" (??0?$DLNode@H@ @QAE@H@Z) referenced in function "public: void __thiscall DLList<int>::ad dNode(int)" (?addNode@?$DLL ist@H@@QAEXH@Z)

    Note I left the header files out as I don't think the problem is there, but I can include if need be. Any explaination is appreciated:

    Code:
    #include <stdio.h>
    #include "DLList.cpp"
    
    main(){
    	DLList<int> intlist;
    	intlist.addNode(13);
    	printf( "%d", intlist.getFirst() );
    }
    Code:
    #include "DLList.h"
    
    template <class Type>
    DLList<Type>::DLList(){
    	first = 0;
    	last = 0;
    }
    
    template <class Type>
    void DLList<Type>::addNode(Type a){
    	DLNode<int> intlist(a);
    	first = &intlist;
    }
    
    template <class Type>
    Type DLList<Type>::getFirst(){
    	return first->getElement();
    }
    Code:
    #include "DLNode.h"
    
    template <class Type>
    DLNode<Type>::DLNode(Type a){
    	data = a;
    }
    
    template <class Type>
    Type DLNode<Type>::getElement(){
    	return data;
    }
    template <class Type>
    void DLNode<Type>::setElement(Type a){
    	data = a;
    }
  • plemoine
    New Member
    • Jun 2007
    • 15

    #2
    You should include DLList.h, and not .cpp! In fact, you don't need .cpp file for your template implementation: all the template methods figure inside the header, with constructor and all.

    So, as soon as you declare a variable in a templated class - like DLNode<int> - then a new class is generated.

    You can view templates as "molds" to generate classes. They are not classes by themselves.

    Originally posted by raistlinx
    Hi. I'm trying to write series of classes using templates and am running into what must be a simple error. I'm a bit of a noob so I apologize in advance if this kind of question is not welcome here.

    When I try to run the program below i get the following error during build (using Visual Studio).

    Data Structures error LNK2019: unresolved external symbol "public: __thiscall DLNode<int>::DL Node<int>(int)" (??0?$DLNode@H@ @QAE@H@Z) referenced in function "public: void __thiscall DLList<int>::ad dNode(int)" (?addNode@?$DLL ist@H@@QAEXH@Z)

    Note I left the header files out as I don't think the problem is there, but I can include if need be. Any explaination is appreciated:

    Code:
    #include <stdio.h>
    #include "DLList.cpp"
    
    main(){
    	DLList<int> intlist;
    	intlist.addNode(13);
    	printf( "%d", intlist.getFirst() );
    }
    Code:
    #include "DLList.h"
    
    template <class Type>
    DLList<Type>::DLList(){
    	first = 0;
    	last = 0;
    }
    
    template <class Type>
    void DLList<Type>::addNode(Type a){
    	DLNode<int> intlist(a);
    	first = &intlist;
    }
    
    template <class Type>
    Type DLList<Type>::getFirst(){
    	return first->getElement();
    }
    Code:
    #include "DLNode.h"
    
    template <class Type>
    DLNode<Type>::DLNode(Type a){
    	data = a;
    }
    
    template <class Type>
    Type DLNode<Type>::getElement(){
    	return data;
    }
    template <class Type>
    void DLNode<Type>::setElement(Type a){
    	data = a;
    }

    Comment

    • raistlinx
      New Member
      • Jul 2007
      • 14

      #3
      Thanks for the help. I moved everything into the header file butnow I seem to have a problem accessing data. intlist.getFirs t() doesn't return the data. I first thought this was a problem with DLList accessing DLNode methods and tried adding it as a friend to DLNode but got the same results.

      Again, this must be something trivial?

      Code:
      #include <stdio.h>
      #include "DLList.h"
      
      main(){
      	DLList<int> intlist;
      	intlist.addNode(9);
      	printf("%d", intlist.getFirst());
      }
      Code:
      #include "DLNode.h"
      
      template <class Type>
      class DLList{
      public:
      	DLList(){
      	first = 0;
      	last = 0;
      }
      	void addNode(Type a);
      	Type getFirst();
      
      private:
      	DLNode<Type> *first;
      	DLNode<Type> *last;
      };
      
      template <class Type>
      void DLList<Type>::addNode(Type a){
      	DLNode<int> intlist(a);
      	first = &intlist;
      }
      
      template <class Type>
      Type DLList<Type>::getFirst(){
      	return first->getElement();
      }
      Code:
      template <class Type> class DLList;
      
      template <class Type> class DLNode {
      	friend class DLList<Type>;
      
      public:
      	DLNode(Type a){data = a;}
      	Type getElement();
      	void setElement(Type a);
      
      private:
      	Type data;
      	DLNode *prevNode;
      	DLNode *nextNode;
      };
      
      template <class Type>
      	Type DLNode<Type>::getElement(){
      		return data;
      	}
      template <class Type>
      	void DLNode<Type>::setElement(Type a){
      		data = a;
      	}

      Comment

      • raistlinx
        New Member
        • Jul 2007
        • 14

        #4
        Ok, I did some more digging and it is not a problem with the templates it is a problem with the classes. I assume "first" has gone out of scope after the addNode method??? (note I took out the templates stuff for readability)


        Code:
        #include <stdio.h>
        #include "DLListint.h"
        
        main(){
        	DLListint intlist;
        	intlist.addNode(9);
        	printf("%d", intlist.getFirst());
        	getchar();
        }
        Code:
        #include <stdio.h>
        
        class DLNodeint {
        	friend class DLListint;
        
        public:
        	DLNodeint(int a){data = a;}
        	int getElement();
        	void setElement(int a);
        
        private:
        	int data;
        	DLNodeint *prevNode;
        	DLNodeint *nextNode;
        };
        
        	int DLNodeint::getElement(){
        		return data;
        	}
        	void DLNodeint::setElement(int a){
        		data = a;
        	}
        
        class DLListint{
        public:
        	DLListint(){
        	first = 0;
        	last = 0;
        }
        	void addNode(int a);
        	int getFirst();
        
        private:
        	DLNodeint *first;
        	DLNodeint *last;
        };
        
        void DLListint::addNode(int a){
        	DLNodeint intlist(a);
        	first = &intlist;
        	printf("%d\n", a); // works
        	printf("%d\n", intlist.getElement()); // works
        	printf("%d\n", first->getElement()); // works
        }
        
        int DLListint::getFirst(){
        	printf("%d\n", first->getElement()); // wrong
        	return first->getElement();
        }

        Comment

        • raistlinx
          New Member
          • Jul 2007
          • 14

          #5
          Problem solved: DLNode<int> *intlist = new DLNode<int>(a);


          DOH...

          Comment

          Working...