Function Template

This is a discussion on Function Template within the C++ Programming forums, part of the General Programming Boards category; How to declare function template inside class? Code: class CTime { public: CTime(int mn, int hr); template<typename Any> void max(Any ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Function Template

    How to declare function template inside class?
    Code:
    class CTime  
    {
    public:
    	CTime(int mn, int hr);
    
    	template<typename Any>
    	  void max(Any no);
    }; 
    
    In the cpp file I am implementing max.
    
    When I create instance in main and try calling max:
    
    
    int main()
    {
    	CTime T(1,2);
    
    	T.max(3);
    
    	return 0;
    
    }
    
    I get a linking error for max.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The implementation for templates must also be in the header file.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    41
    Here is my .h file:

    Code:
    class CTime  
    {
    public:
    	CTime(int mn, int hr);
    	~CTime();
    	
    	template<typename Any>
    		void max(Any no);
    };
    This is cpp file:

    Code:
    #include "Time.h"
    
    
    CTime::CTime(int mn, int hr)
    {
    
    }
    
    CTime::~CTime()
    {
    
    }
    
    
    template<typename Any>
    void CTime::max(Any no)
    {
    }
    and this is main:

    Code:
    #include "Time.h"
    
    int main()
    {
    	CTime T(1,2);
    
    	T.max(3.8);
    
    	return 0;
    
    }

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    41
    implementing in header ... makes the code look bad.. n does that mean that template function becomes inline?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Ehh... you template the class definition not the prototypes inside of it and you name the template before the scope resolution. Then you have to name a type on your objects.
    Code:
    template<typename Any>
    class CTime  
    {
        public:
    	CTime(int mn, int hr); // Why is the constructor accepting ints, shouldn't it
            // accept the templated type?
    	~CTime();
    
    	void max(Any no);
    };
    Code:
    #include "Time.h"
    
    template<typename Any>
    CTime<Any>::CTime(int mn, int hr)
    {
    
    }
    template<typename Any>
    CTime<Any>::~CTime()
    {
    
    }
    
    
    template<typename Any>
    void CTime<Any>::max(Any no)
    {
    }
    Code:
    #include "Time.h"
    
    int main()
    {
    	CTime<int> T(1,2);
    
    	T.max(3.8);
    
    	return 0;
    
    }
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    41
    oops!!

    Now I get the following errors:

    maincpp.obj : error LNK2001: unresolved external symbol "public: __thiscall CTime<int>::~CTime<int>(void)" (??1?$CTime@H@@QAE@XZ)
    maincpp.obj : error LNK2001: unresolved external symbol "public: void __thiscall CTime<int>::max(int)" (?max@?$CTime@H@@QAEXH@Z)
    maincpp.obj : error LNK2001: unresolved external symbol "public: __thiscall CTime<int>::CTime<int>(int,int)" (??0?$CTime@H@@QAE@HH@Z)

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Are you sure your linking the Time.c object? I'm sorry I'm not seeing the linker error here, I must be having a block in the brain. Give me a few minutes.
    Last edited by SlyMaelstrom; 06-16-2006 at 03:34 PM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    41
    Hey Sly..

    i don't think i missed anything.. btw, did u try compiling the code at ur end? did it work?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Oh now I know. There is some issues with templating when you can't seperate the defintion from the implementation and maintain the template. If you want to use a template, the implementation has to be in the header. Something like that. Just make sure you use a header guard.

    That's the issue here. I'm sure someone else will shed light on the full details of the subject.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    41
    Yes, declaring in header makes it work.. but does that make the function inline?

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    41
    sorry implementing in header makes it work ...but does that make the function inline?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No. If you put the implementation into the class definition, that will be the same as marking it as an inline function. But if you just move what you had in the source file to the bottom of the header file, it will not be marked for inlining.

    Another option is to leave the implementation in the source file and #include the source file at the bottom of the header. This would only be a good idea if the source file only contained definitions for templated classes and functions.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    41
    Are you suggesting something like this?


    .h file:
    Code:
    class CTime  
    {
    public:
    	CTime(int mn, int hr);
    	~CTime();
    	
    	template<typename Any>
    		void max(Any no);
    };
    
    template<typename Any>
    void CTime::max(Any no)
    {
    }
    This is cpp file:


    Code:
    #include "Time.h"
    
    
    CTime::CTime(int mn, int hr)
    {
    
    }
    
    CTime::~CTime()
    {
    
    }
    and this is main:


    Code:
    #include "Time.h"
    
    int main()
    {
    	CTime T(1,2);
    
    	T.max(3.8);
    
    	return 0;
    
    }
    I need to modify the way I call max, cause it gives me error saying:
    Failed to specialize function template 'void __thiscall CTime::max(Any)'
    With the following template arguments:
    'int'

    How do I call the template function?

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    If your only goal here is to template the function, then I don't understand why it's in the class, at all.
    Sent from my iPadŽ

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    What's the body of the function? Sounds like it couldn't substitue int into it.

    Including .cpp files is a bad idea: they tend to get picked up by build tools and you get multiple definition errors. Give the files a different extension; ipp is popular.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. function template question
    By Thantos in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21