Thread: Why won't templated function work

  1. #1
    some loser
    Guest

    Why won't templated function work

    I don't understand why the following doesn't work

    Code:
    #ifndef ITERATOR_H
    #define ITERATOR_H
    
    #include "Main.h"
    
    /*
    	ExtractFromFile(
    */
    template<class	X>
    void	ExtractFromBuffer(byte	*, X *);
    
    #endif
    the source file
    Code:
    #include "Iterator.h"
    template<class X>
    void	ExtractFromBuffer(byte *ptr, X *a)
    {
    	a = (X*)ptr;
    	ptr += sizeof(X);
    }
    and where it is used:

    Code:
    	mpCDCL	*cdcl= new CDCL;
    	ExtractFromBuffer(ptr, cdcl);
    I know ptr has been initialized properly (points to a byte array that has already been initialized).

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    All of the source code for the templated function needs to be in the header file. Templated functions cannot be implemented the way you are trying to do it. Compiling source code files containing templates by themselves will not actually generate any code. This will cause problems during the linking step because the linker will not be able to find the code for the function. Putting the code for the template in the header file and then including that header file in the source files where it is needed will make this work.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    some loser
    Guest
    Oh

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by hk_mp5kpdw
    Templated functions cannot be implemented the way you are trying to do it.
    They can, at least in theory, be implemented that was using the 'export' keyword.
    However, not many compilers support this (I only know one, Comeau C++)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    some loser
    Guest
    I tried changing this to void* because I've seen that in the fread function parameter, but it deosn't work. how is it possible to have void* be passed in as a data type to any function? Is that a C convention that was not passed over to C++ (replaced by templates?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM