-
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.
-
The implementation for templates must also be in the header file.
-
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;
}
-
implementing in header ... makes the code look bad.. n does that mean that template function becomes inline?
-
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;
}
-
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)
-
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. :)
-
Hey Sly..
i don't think i missed anything.. btw, did u try compiling the code at ur end? did it work?
-
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.
-
Yes, declaring in header makes it work.. but does that make the function inline?
-
sorry implementing in header makes it work ...but does that make the function inline?
-
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.
-
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?
-
If your only goal here is to template the function, then I don't understand why it's in the class, at all.
-
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.