Thread: Template + Multifile

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    Template + Multifile

    Hi friends
    I wrote some program with structue like this :
    but it does'nt work , and I don't know why
    please help me , Thank you

    main.cpp
    Code:
    #include "Unit10.h"
    #include <conio.h>
    
    int main(int argc, char* argv[])
    {
    	tst<int> obj(10);
    	obj.show();
    	getch();
    	return 0;
    }
    Implement (Unit9.cpp) :
    Code:
    #include <iostream>
    using std::cout ;
    
    #include "Unit10.h"
    template <typename T>
    void tst<T>::show()
    {
    	cout << i ;
    }
    and interface:
    Code:
    #ifndef Unit10H
    #define Unit10H
    template <typename T>
    class tst
    {
    	public :
    		T i ;
    		tst<T>(T n=0) : i(n) {}
    		void show() ;
    
    };
    #endif
    error :
    Code:
    [Linker Error] Error: Unresolved external 'tst<int>::show()' referenced from C:\UNIT9.OBJ
    and I checked it in C++Builder , dm , DevC++
    but the result of all of them were similar.
    Last edited by Mehdi; 11-02-2006 at 06:15 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Most compilers don't support template function defintions in a separate file (the two you've mentioned definitely don't). Move the template function definition into the header file, Unit10.h

    As an aside, take a look at this faq, too.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM