473,288 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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

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 TYPEclass 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<intobj;
obj.func();
}
**************************

May 4 '07 #1
8 7605

ni**********@st.com 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 TYPEclass 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<intobj;
obj.func();
}
**************************
On what compiler you have compile-time error ? And what is error
message ?

May 4 '07 #2

Hari wrote:
ni**********@st.com 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 TYPEclass 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<intobj;
obj.func();
}
**************************

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)'

May 4 '07 #3
ni**********@st.com wrote:
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 TYPEclass 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<intobj;
obj.func();
The compiler must be able to see the definition of A here.

--
Ian Collins.
May 4 '07 #4
On 4 Maj, 10:58, nishit.gu...@st.com wrote:
Hari wrote:
nishit.gu...@st.com 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 TYPEclass 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<intobj;
obj.func();
}
**************************
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)'
This is in the FAQ, section 35.13, you should take some time to read
it: http://www.parashift.com/c++-faq-lite/

--
Erik Wikström

May 4 '07 #5

ni**********@st.com je napisao:
Hari wrote:
ni**********@st.com 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 TYPEclass 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<intobj;
obj.func();
}
**************************
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)'
You wrote
>following problem is not linking (undefined reference of func)but
getting copiled
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

May 4 '07 #6
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,
ni**********@st.com je napisao:
Hari wrote:
ni**********@st.com 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 TYPEclass 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<intobj;
obj.func();
}
**************************
>
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)'

You wrote
following problem is not linking (undefined reference of func)but
getting copiled

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
May 4 '07 #7
ni**********@st.com wrote:
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.

--
Ian Collins.
May 4 '07 #8

Ian Collins wrote:
ni**********@st.com wrote:
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.

--
Ian Collins.
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

May 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
1
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
3
by: Patrick Guio | last post by:
Hi, I have trouble to compile the following piece of code with g++3.4 but not with earlier version // Foo.h template<typename T> class Foo { public:
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
4
by: James Aguilar | last post by:
Guys, When I specialize a template class member function (I.e. a member function of a template class) based on that class' type, bad things happen. Here's some code: ---- test_header.h...
5
by: rf | last post by:
As I understand it, it is legal to have a template member function of a non-template class, as in the following example: class XYZ { public: template <class Tvoid fn(T* ptr) { T val = *ptr;...
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.