Thread: Linker Error In Template Code

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Linker Error In Template Code

    Hi team

    I was working on template and found the linker error while compiling the code.

    Below is the code:

    Code:
    //Template1.h: Header file where template define
    
    #ifndef __Template1_H__  
    #define __Template1__  
                          
    
    
    template <typename T,typename T1> class A
    {
     
    	T a;
    	T1 b;
    
    	public:
    		A(T ,T1 ) ;
    
    		void getData();
    		
    };
    #endif // __

    Code:
    //Template1.cpp where class define
    
    #include<iostream>
    #include<string>
    
    #include "Template1.h"
    
    using namespace std;
    
    
    
    
    
    template<typename T,typename T1> A<T,T1>:: A(T x,T1 y)
    {
     a = x;
     b = y;
    }
    
     template<typename T,typename T1> void A<T,T1>:: getData()
    {
     cout<< a << b;
    }
    Code:
    // Main Class from where code called:Template2.cpp
    
    #include<iostream>
    #include<string>
    
    #include "Template1.h"
    
    int main()
    {
    
       A<int,char> f(1,'d');
    
       f.getData();
    
       
       getchar();
       return 0;
    }
    I am getting below linker error:

    Template2.obj : error LNK2001: unresolved external symbol "public: void __thiscall A<int,char>::getData(void)" (?getData@?$A@HD@@QAEXXZ)

    Template2.obj : error LNK2001: unresolved external symbol "public: __thiscall A<int,char>::A<int,char>(int,char)" (??0?$A@HD@@QAE@HD@Z)
    Can anyone help in this.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    This is wrong:
    Code:
    #ifndef __Template1_H__ 
    #define __Template1__
    The names don't match, and even if they did, they are reserved to the implementation so you should not use them unless you are writing the implementation. (Names that contain consecutive underscores, or that begin with an underscore followed by an uppercase letter, are reserved to the compiler/standard library implementation for any use.)

    Now, to answer your question itself, read the answer to this FAQ: Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks for reply.

    I was too reading that link. I am using class, so how can I declare member function as extern? As shown in link.
    Is there some other syntax for Class ?

    Could you please help.

    Thanks
    Nickman

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    I suggest just defining the member functions of the class template in the header, e.g.,
    Code:
    #ifndef NICKMAN_TEMPLATE1_H_
    #define NICKMAN_TEMPLATE1_H_
    
    #include <iostream>
    
    template <typename T, typename T1>
    class A
    {
    public:
        A(T x, T1 y) : a(x), b(y) {}
    
        void getData()
        {
            std::cout << a << b;
        }
    private:
        T a;
        T1 b;
    };
    
    #endif
    By the way, getData is not const-correct and probably should be renamed to printData. Also, instead of using std::cout, I suggest that you have a std::ostream& parameter... in which case you could consider overloading operator<< instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Yes. By defining in header it was working fine. But I want to try the solution in the link. But it was not working.
    Am I doing something wrong?

    Also,as link suggest that make function as extern will not work in case of class member function..right?

    thanks
    Nickman

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    I tried this in Template1.h


    I tried this in Template1.h

    Code:
     template<typename T,typename T1> extern  void getData();
    In Template1.cpp

    Code:
    template<typename T,typename T1> void getData();
    And got error:
    c:\c++\template1\template1.h(15) : error C2720: 'getData' : 'extern' storage-class specifier illegal on members

    I tried this also:

    Code:
    extern void getData();
    But same result.

    Thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    No, an extern template is not what you want here. Refer to Stroustrup's C++11 FAQ entry on extern templates.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I was too reading that link. I am using class, so how can I declare member function as extern? As shown in link.
    O_o

    You should post the link that uses `extern' with templates.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2013, 08:50 AM
  2. X server slaps some template code with a "fatal IO error"
    By Yarin in forum Linux Programming
    Replies: 8
    Last Post: 08-24-2009, 08:47 AM
  3. specify linker switch in code
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 02-15-2009, 04:06 PM
  4. Linker problem with template traits/policy on MSVS 2005
    By MarkZWEERS in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2008, 04:01 AM
  5. Template linker error
    By Thantos in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2004, 08:06 PM