Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Basic doubt with Class template

Basic doubt with Class template

Scheduled Pinned Locked Moved ATL / WTL / STL
c++
22 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H hrishi321

    I think there is a misunderstanding. Sorry if my words were not able to explain my doubt. If you please refer to my main-doubt

    Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file.

    The code which you have provided works absolutely fine, in just one file. Just that I wanted to know, if there is any way to separate the definitions of the method in an another .cpp file. (Just like a normal class where we declare in .h and implement in .cpp)

    template <class T>
    void CMyTemplate<T>::Method1(T& TDummy)
    {
    TDummy = TDummy + 1;
    }

    and the declaration in an separet .h file

    template <class T>
    class CMyTemplate
    {
    public:
    virtual void Method1(T& TDummy);
    };

    thanks in advance hrishi

    O Offline
    O Offline
    Orjan Westin
    wrote on last edited by
    #21

    If you are still looking for an answer, I posted an article explaining this just today. http://www.codeproject.com/Articles/236927/All-your-base64-are-different-to-us In short, there is a way, provided you know all template types you will be using. I'll copy the example I had in the article here:

    -------------------------------------
    -- In my_template.h file
    ...
    template
    T my_temp_func(const T& t);


    -- In my_template.cpp file

    #include "my_template.h"

    // Definition
    template
    T my_temp_func(const T& t)
    {
    return t;
    }

    // Instantiation declaration
    int my_temp_func(const int& t);


    -- In using_my_template.cpp
    #include "my_template.h"
    ...
    int i = my_temp_func(4); // works
    string s = my_temp_func("Abob"); // link error

    In the example above, the declaration in the last line of my_template.cpp tells the compiler there’ll be a variant of the template function that uses int as template parameter. Okay, says the compiler, I’ll put an inline copy there. Since the generic definition is right there in the same compilation unit (ie my_template.cpp), this is something the compiler can do – it has all the information it needs. The result of that is that in the compiled file (probably called my_template.obj) there is now a function that has the signature int my_temp_func(const int& t). This is a fully defined specialisation of a template function, so to the linker it looks just like a normal function. However, the linker won’t be able to find a string specialisation, so this will generate a linker error. In

    1 Reply Last reply
    0
    • H hrishi321

      Hello everyone Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file. I know the way, wherein, we define all the methods in a file( with extension other then .cpp), and include the file at the end of the header file of the template class. But this method looks no good, is there any clean and usual way just like other class. thanks in advance Hrishi

      B Offline
      B Offline
      Bram van Kampen
      wrote on last edited by
      #22

      Your basic problem appears to be to understand how Header Files, cpp Files, the Compiler and the Linker work. First the Compiler. The Compiler builds an Object File. This is a Binary representation of a cpp File. It containd the CPP Code you wrote, converted to machine code as well as a whole raft of information needed later. The Compiler contains a thing, called a Pre-Processor. It prepares your CPP file before actual compilation. Conceptually it produces a New Source File. It copies your file Line by Line. but if it finds a statement like #include "MyBeautifulHdr.h", it does not copy the include statement, but, instead writes the entire contents of "MyBeautifulHdr.h" at the location of the '#include' statement. When it encounters a #define FOO 1 statement, it undertakes to replace henceforth every future occurrence of 'FOO' with 1. It goes on like that at infinitum, and the final file offered to the compiler has all '#includes, #defines, #if's etc resolved. At that stage the compiler does not know ( and does not care) from what file what item came from, (except for error reporting). It just builds the Object File, (if it Can) if it cannot, you get an error. Each of the Object Files generated roughly correspond to the code you wrote in a cpp file. An extern declaration is a promise to the compiler that somewhere in an other file or library, an item of that specification exists. The Linker tries to build an executable out of all those object files. In doing so, it may also include Object files written by others, e.g. those written by Microsoft, which came with your compiler package. To make that process more efficient, multiple obj files can be gathered together into a library (.lib) file If the linker cannot find a named item, or, finds more than one, the linker will fail building the executable. Getting a Full understanding of how your tools work will answer your queery. regards, :)

      Bram van Kampen

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups