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


N

nishit.gupta

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();
}
**************************
 
Ad

Advertisements

H

Hari

(e-mail address removed) je napisao:
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();
}
**************************

On what compiler you have compile-time error ? And what is error
message ?
 
N

nishit.gupta

Hari said:
(e-mail address removed) je napisao:

On what compiler you have compile-time error ? And what is error
message ?
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)'
 
I

Ian Collins

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;
Never, ever put a using directive in a header.
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";
}

Nothing is instantiated here.
*********************************
#main.cpp
#include"temp.h"

int main(){

A<int> obj;
obj.func();

The compiler must be able to see the definition of A here.
 
H

Hari

(e-mail address removed) je napisao:
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)'

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
 
Ad

Advertisements

N

nishit.gupta

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,
 
I

Ian Collins

Ian,
thanx for telling syntax mistakes......but my question was totally
different from what you explained...

Please don't top-post or use silly txtspk abbreviations. I also told
you why it didn't work.
 
Ad

Advertisements

N

nishit.gupta

Ian said:
Please don't top-post or use silly txtspk abbreviations. I also told
you why it didn't work.
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top