Click to See Complete Forum and Search --> : Can a template function be a member of the class? (with code)
zqliu
10-18-2001, 09:10 AM
I don't know whether the following code are ok, but there are errors when
I try to call the function! Help me!
******************************************************
The template function is in header file:
template<class T>
void CImg::GetImgPanel(int i, T **& ptr)
{
switch(typeid(T))
{
typeid(int) : ...... break;
typeid(BYTE) : ...... break;
typeid(float) : ...... break;
}
......
}
*****************************************************
CImg a;
int ** pa;
a.GetImgPanel(1, pa); =====> ERROR
******************************************************
error C2893: Failed to specialize function template 'void __thiscall CImg::GetImgPanel(int,T
**& )'With the following template arguments: 'int'
*******************************************************
Thanks
www.exontrol.com
10-18-2001, 11:44 AM
At the first sight, the CImg s a template function. Whe you are trying to
instanciate it, you have to call its arguments ( you have no default ), like
CImg<int> or whatever
Mike
www.exontrol.com
"zqliu" <zqliu_msr@hotmail.com> wrote:
>
>I don't know whether the following code are ok, but there are errors when
>I try to call the function! Help me!
>******************************************************
>The template function is in header file:
>
>template<class T>
>void CImg::GetImgPanel(int i, T **& ptr)
>{
> switch(typeid(T))
> {
> typeid(int) : ...... break;
> typeid(BYTE) : ...... break;
> typeid(float) : ...... break;
> }
> ......
>}
>*****************************************************
>CImg a;
>int ** pa;
>a.GetImgPanel(1, pa); =====> ERROR
>******************************************************
>error C2893: Failed to specialize function template 'void __thiscall CImg::GetImgPanel(int,T
>**& )'With the following template arguments: 'int'
>*******************************************************
>Thanks
>
James Curran
10-18-2001, 07:04 PM
I'm not exactly sure why what you are trying failed, but it's just as
well, because it's not a very good design anyway. Never have the program do
at runtime, what you can have the compiler to at compile time:
void CImg::GetImgPanel(int i, int**& ptr)
{ ....}
void CImg::GetImgPanel(int i, BYTE**& ptr)
{ ....}
void CImg::GetImgPanel(int i, float**& ptr)
{ ....}
Since your design already requires you to write a separate case statement
for each possible date type passed, you might as well just overload the
function and have the compiler sort out the data types beforehand.
This have the added bonus that if you tried call it as, say,
char **ptr;
GetImgPanel(5,ptr);
My way would give a compile time error; yours would just silently fail at
runtime.
--
Truth,
James Curran
www.NJTheater.com (Professional)
www.NovelTheory.com (Personal)
www.BrandsForLess.Com (Day Job)
"zqliu" <zqliu_msr@hotmail.com> wrote in message
news:3bced4cc$1@news.devx.com...
>
> I don't know whether the following code are ok, but there are errors when
> I try to call the function! Help me!
> ******************************************************
> The template function is in header file:
>
> template<class T>
> void CImg::GetImgPanel(int i, T **& ptr)
> {
> switch(typeid(T))
> {
> typeid(int) : ...... break;
> typeid(BYTE) : ...... break;
> typeid(float) : ...... break;
> }
> ......
> }
> *****************************************************
> CImg a;
> int ** pa;
> a.GetImgPanel(1, pa); =====> ERROR
> ******************************************************
> error C2893: Failed to specialize function template 'void __thiscall
CImg::GetImgPanel(int,T
> **& )'With the following template arguments: 'int'
> *******************************************************
> Thanks
>
devx.com
Copyright Internet.com Inc. All Rights Reserved