Thread: Templates: why can't compiler see the definition

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    Templates: why can't compiler see the definition

    I write a simple program
    templatex.h
    Code:
    #ifndef TEMPLATEXH
    #define TEMPLATEXH
     
    class Templatex
    {
    public:
    	void print();
    	template <typename T> 
    	T const& getmax(T const &x , T const &y);
    };
    #endif
    templatex.cpp
    Code:
    #include <iostream>
    #include "templatex.h"
    
    using namespace std;
    
    void Templatex::print()
    {
    	cout << "print";
    }
    template <typename T>
    T const& Templatex::getmax(const T &x, const T &y)
    {
    	if ( x>y )
    		return x;
    	else
    		return y;
    };
    templateuse.cpp
    Code:
    #include <iostream>
    #include "templatex.h"
    using namespace std;
    
    int main()
    {
    	Templatex* t=new Templatex();
    	t->print();
    }
    Here in main why can not we call t->getmax();
    Why can not compiler see the definition befaore main?

    they said that:
    "When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition."

    Can someone clarify this subject please .I don't understand.
    I am looking for your answers.
    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    People usually either #include the .cpp file, or more often, the implementation of the template class is contained entirely in the header file.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Templates are not compiled until they are used, so the entire template, not just the prototypes, must be in the header file. To separate parallel normal classes, the definitions can be put into a *.cpp file. You would than #include the *.cpp file in the header.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thanks IfYouSaySo
    thanks King Mir
    I think i don't understand this because i don't know what compiler do while compiling and the factor of the .h and .cpp files.In my example where does compiler start to compile.We have 3 files :
    1-)templatex.h
    2-)templatex.cpp (implemantaion of .h file)
    3-)templateuse.cpp.(main)
    What is its compiling order.I aassume that(i don't know it is right or wrong please correct:

    First start templateuse.cpp and in its preprocessors there is an include file that templatex.h.So before main it compile templatex.h and its implemention file templatex.cpp.So we have binary format of our template function.

    But this must be wrong.Because some errors occurs.

    1-)What are the wrongs in my definition.What is compiling order's of the .cpp files and .h files in c++ projects.

    2-)For header files(e.g:templatex.h).How does compiler knows its definition is in the templatex.cpp .What is happening under the hood?Because their names are the same "templatex" or something different?(this question's answer is very important for me).

    Thanks again for your answers.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    *.h files and anything included in them are not compiled. Neither are *.cpp files with only teplate class definitions (although your compiler may compile an empty *.o in that case).

    In your example I would #include "templatex.cpp" at the end of templatex.h, which is then included in templateuse.cpp.

    Also, you need to make your entire class into a template, not just getmax() like this:
    Code:
    	template <typename T> 
    class Templatex
    {
    public:
    	void print();
    	T const& getmax(T const &x , T const &y);
    };
    2-)For header files(e.g:templatex.h).How does compiler knows its definition is in the templatex.cpp .What is happening under the hood?Because their names are the same "templatex" or something different?(this question's answer is very important for me).
    It knows the definition is in templatex.cpp because that's where it first encounters when reading templateuse.cpp. Remember, a #include statement effectively inserts the included file into the file doing the including. So #including templatex.cpp has the same effect as coping its contents in its place.

    Also, templatex can have any valid c++ exetention, including *.h.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok friends i understand it now.
    Thanks
    good works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Problem with overloaded function templates
    By New++ in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:00 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM