.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   Calling template member functions?? (http://forums.bots-united.com/showthread.php?t=886)

Rick 25-02-2004 13:22

Calling template member functions??
 
I don't get it.....
Code:

template <class Dummy> class TTest
{
public:
        template <typename C> void CallFunc(C *s, bool(C::*pMemberFunc)(void))
        {
                s->*pMemberFunc();
        };
};

class CTest
{
public:
        void bleh(void)
        {
                printf("Bleh\n");
        };
};

int main()
{
        TTest<short> *pTest = new TTest<short>;
        CTest Test2;
        pTest->CallFunc<CTest>(pTest, CTest::bleh);

        return 0;
}

gives me the error
Quote:

error C2275: 'CTest' : illegal use of this type as an expression
E:\bot_tools\test_proggie\test3\test3\test3.cpp(44 2) : see declaration of 'CTest'
Acoording to my c++ book I should do this to call the function
Code:

pTest->template CallFunc<CTest>(pTest, CTest::bleh);
but then I get this error;
Quote:

error C2951: template declarations are only permitted at global or namespace scope
Can anyone help me :|

stefanhendriks 25-02-2004 17:08

Re: Calling template member functions??
 
i have no clue what templates do, to me the above code looks like bloatware?

Pierre-Marie Baty 25-02-2004 17:48

Re: Calling template member functions??
 
LOL @Stefan :D He's talking about the C++ templates, which are a pre-made set of types and tools that can supposedly be used out of the box in any C/C++ program. It features linked lists, binary trees, hashtables, etc.

@Rick: never used them. Dunno :) Perhaps you can get a hint on how to use passed pointers to reference functions by looking at the AStarSearch() function in Count Floyd's POD-bot source code. He passes functions addresses as parameters of this function. It sounds like a pointer indirection problem, anyhow.

Rick 25-02-2004 17:49

Re: Calling template member functions??
 
ehh templates are normal c++ Stefan :) So is the rest except the line whith the error I guess.

http://www.cplusplus.com/doc/tutorial/tut5-1.html

meh to late :)

I did some searching the problem is that int his line;
pTest->CallFunc<CTest2>(pTest, CTest::bleh);

the compiler sees the '<' and the '>' as less-and greater then operators...
After I searched a bit in my book it sayed you have to use the template keyword like I did but it seems that msvc 6.0 this doesn't support :(

stefanhendriks 25-02-2004 17:51

Re: Calling template member functions??
 
yes i know C++ has templates, still sounds like bloatware. I have read enough theories about "you can use your classes in other programs" , and i only got that working ONCE in my entire life...

Rick 25-02-2004 18:01

Re: Calling template member functions??
 
heh problem solved....
http://forums.thewavelength.net/inde...=0&#entry83190

@$3.1415rin 25-02-2004 19:02

Re: Calling template member functions??
 
@stefan : template are often a great feature, I use them in my AStar code as you might have seen. But don't try to use them when wanting them to access via dynamic linking from a .dll for example, that's scary and results in more work than using another possibility, I once tried that in a NN - simulator ...

and yes, rick, that <...> stuff is mostly only needed when instantiating data elements. your problem is of course a bit more complicated, a template function inside a template class ...

maybe you should try to compose classes using virtual base classes and multilayered template constructs .... I guess that'd be almost the summit of crazyness ... I'm using some virtual base class in my bot - in theory everything is fine, but some fuckin' compilers don't like each part of those, creating warnings etc.. you won't guess which one ;)

Rick 25-02-2004 19:50

Re: Calling template member functions??
 
hmm virtual base classes is one of those subjects i haven't read much on yet ;)
But its working now, as long the compiler can find the right types for the variabeles all works fine.


All times are GMT +2. The time now is 04:50.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.