LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   template class (https://www.linuxquestions.org/questions/programming-9/template-class-259337/)

ckcheung0927 11-25-2004 08:53 PM

template class
 
Dear All,

I have problem on programming in template class.
Code:

#include <stdio.h>
template <class ArgType>
class ClassA
{
public:
        template <class ClsType>
                void Func( ClsType* pObj, void (ClsType::*pClsFunc)(ArgType), ArgType arg )
        {
                (pObj->*pClsFunc)( arg );
        }       
};

class ClassB
{
public:
        void Func( int a )
        {
                printf( "a=%d\n", a );
        }
};

int main( int argc, char* argv[] )
{
        ClassA<int> a;
        ClassB b;

        a.Func( &b, ClassB::Func, 1 );

        return 0;
}

while i use g++ to compile the program, i got the following message

test.cpp: In function `int main(int, char**)':
test.cpp:28: no matching function for call to `ClassA<int>::Func(ClassB*,
<unknown type>, int)'
test.cpp:9: candidates are: void ClassA<ArgType>::Func(ClsType*, void
(ClsType::*)(ArgType), ArgType) [with ClsType = ClassB, ArgType = int]


But this program can be successfully compiled in VC++.

Please Help.

Reply me please

MartinN 11-26-2004 02:03 PM

You need to send a function pointer as argument in main. It should look like this:
Code:

int main( int argc, char* argv[] )
{
        ClassA<int> a;
        ClassB b;

        a.Func( &b, &ClassB::Func, 1 );
        // Note the '&' before ClassB::Func

        return 0;
}

Martin

XavierP 11-26-2004 02:51 PM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

ashirazi 11-28-2004 04:59 PM

looks like you're kinda new to templates. I use this tutorial which helped me.

[LINK]http://babbage.cs.qc.edu/STL_Docs/templates.htm[/LINK]

raven


All times are GMT -5. The time now is 04:45 AM.