why template class member fuction definition should be in header file??

Discussion in 'C++' started by nishit.gupta, May 4, 2007.

  1. nishit.gupta

    nishit.gupta Guest

    I was having a problem with template class memer function definition ,
    so i serched the net
    and find that template class member fuction definition should be in
    header file else compilation
    will be successful but not linking.
    Can somebody help me in telling the exact reason why compiler cannot
    find the reference of
    template class member function definition defined in cpp file??
    following problem is not linking (undefined reference of func)but
    getting copiled

    *************************
    #test.h
    #include<iostream>
    using namespace std;

    template<class TYPE> class A {
    public:
    TYPE func();
    };
    ******************************
    #test.cpp#include<iostream>
    #include "temp.h"

    using namespace std;
    template <class TYPE>
    TYPE A<TYPE>::func()
    {
    cout<<"Hello\n";
    }
    *********************************
    #main.cpp
    #include"temp.h"

    int main(){

    A<int> obj;
    obj.func();
    }
    **************************
     
    nishit.gupta, May 4, 2007
    #1
    1. Advertisements

  2. nishit.gupta

    Hari Guest

    je napisao:
    On what compiler you have compile-time error ? And what is error
    message ?
     
    Hari, May 4, 2007
    #2
    1. Advertisements

  3. nishit.gupta

    nishit.gupta Guest

    3.2.2 , 2.95.3, 3.2*

    just do:
    g++ -c main.cpp -> successful compilation
    g++ -c test.cpp -> successful compilation
    g++ -o test test.o main.o -> undefined reference to
    `A<int>::func(void)'
     
    nishit.gupta, May 4, 2007
    #3
  4. nishit.gupta

    Ian Collins Guest

    Never, ever put a using directive in a header.
    Nothing is instantiated here.
    The compiler must be able to see the definition of A here.
     
    Ian Collins, May 4, 2007
    #4
  5. This is in the FAQ, section 35.13, you should take some time to read
    it: http://www.parashift.com/c++-faq-lite/
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 4, 2007
    #5
  6. nishit.gupta

    Hari Guest

    je napisao:
    You wrote

    This problem is linking problem : g++ -o test test.o main.o ->
    undefined reference to `A<int>::func(void)'

    Problem with template member function defined in separate file is
    object file format. Every .cpp file (unit) is compiled to some kind of
    'object' format. In time of compiling test.cpp compiler does not know
    template type (in your case, you use int in main.cpp). You will not
    have error, becuase you can define templete member function in
    main.cpp.

    Just use one header file for all.

    Best,
    Zaharije Pasalic
     
    Hari, May 4, 2007
    #6
  7. nishit.gupta

    nishit.gupta Guest

    Ian,
    thanx for telling syntax mistakes......but my question was totally
    different from what you explained...
    erric,
    thank you very much for the link, it contains the explanation
    hari,
    this was the sample code which i wrote in a hurry.....but u also dint
    address
    the problem ,thanx for your expalnation also...u alsp got through this
    parashift link ;-)

    Hari wrote:yess,
     
    nishit.gupta, May 4, 2007
    #7
  8. nishit.gupta

    Ian Collins Guest

    Please don't top-post or use silly txtspk abbreviations. I also told
    you why it didn't work.
     
    Ian Collins, May 4, 2007
    #8
  9. nishit.gupta

    nishit.gupta Guest

    sorry for top post again. You only said :
    "The compiler must be able to see the definition of A here. "
    Dont you think that this was implicit for the knd of error i was
    having (i posted that too)
    but i was unware of the reason becuase the code works in case class
    was not a template
    This thing was specific to template.
    anyways, thank you very much
     
    nishit.gupta, May 4, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.