-1

I am facing a problem with templated member function pointer. The code is as shown below.

#include <String>
#include <iostream>
template<typename T>
struct method_ptr
{
    typedef void (T::*Function)(std::string&);
};

template <class T>
class EventHandler
{
private:
    method_ptr<T>::Function m_PtrToCapturer;
};

e:\EventHandler.h(13) : error C2146: syntax error : missing ';' before identifier 'm_PtrToCapturer'

I am facing this error.

Even If I use

method_ptr<EventHandler>::Function m_PtrToCapturer;

as member variable I am getting same error as above.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
TechTotie
  • 127
  • 1
  • 15
  • 1
    Please show the *actual* problem you're trying to solve. – Kerrek SB Feb 18 '14 at 19:16
  • What does "create a variable of this type" refer to?! *Which* type? – Kerrek SB Feb 18 '14 at 19:23
  • Sorry Guys I have a limitation here I can't use C++11 compiler as my project does not entertain it right now. And the actual problem is I want to create a variable of templated function pointer, so that I can store it and call it on some event. ANd while creating the variable I have to use the template instead of class name as it can be used by any class. Hope the problem is clear... – TechTotie Feb 18 '14 at 19:41
  • 1
    No, that's not clear at all. Please present a very short demo (pseudo-code if necessary) of what you would like to write, and indicate the line about which you're unsure (hopefully there's only one). – Kerrek SB Feb 18 '14 at 19:56
  • @TechTotie As I know, in C++98, you should use extra struct... that's why there is template typedef/using in cpp11 – ikh Feb 18 '14 at 19:57
  • suck! stackoverflow steals my sleeping time! morning is coming and I haven't slept.. I'd rather sleep than anything.. – ikh Feb 18 '14 at 20:11
  • To be specific the problem ,I want to store the function pointer that comes to me from any class. How do I do it? I wont be aware of which class will be calling it. Like... I am trying this template method_ptr::Function m_PtrToMemFunc; where T is unknown. It may be Class A calling passing its member function like method_ptr::Function m_PtrToMemFunc; or Class B like method_ptr::Function m_PtrToMemFunc; I wont be knowing which class will be passing its pointer. Like ikh said should I make my class a templated class in this case or is there any other way? – TechTotie Feb 19 '14 at 12:02
  • 1
    @TechTotie: No, no, no, *please* edit your original post to make it self-contained and readable. Look around you, and note how well-formatted other questions are. Spend some time editing everything into shape. You have a live preview to guide you. – Kerrek SB Feb 19 '14 at 15:55
  • @Kerrek: I have re-framed the entire question. Let me know if this is precise and understandable. Sorry to post code in the comment. I thought it would get indented automatically. Will not post code in comments any further.. – TechTotie Feb 19 '14 at 16:42
  • @TechTotie: Cool, thanks. Now I think I know what your question is (and if I'm right, then there are *thousands* of duplicates of this extremely common issue on Stack Overflow). – Kerrek SB Feb 19 '14 at 19:30

3 Answers3

3

Because method_ptr<T>::Function is a dependent name (dependent on T), you need to disambiguate it with typename:

template <class T>
class EventHandler
{
private:
    typename method_ptr<T>::Function m_PtrToCapturer;
//  ^^^^^^^^
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • `typedef typename method_ptr::Function PFunction` will make code more readability. – ikh Feb 20 '14 at 03:11
  • Sorry to open this thread again. How do I call the function. I tried m_PtrToCapturer("Hello"); But results in error "error C2064: term does not evaluate to a function taking 1 arguments" Please help... – TechTotie Feb 26 '14 at 18:43
  • @TechTotie: You need to supply a suitable object on which to call the member function. *Any* tutorial on pointers to member functions will tell you how the syntax works. – Kerrek SB Feb 26 '14 at 19:44
  • @Kerrek: Thanks for your response. I have class Evenhandler's object EventHandler *evthndlr; It is initaliased too. I was trying to call this function pointer like evthndlr->m_PtrToCapturer("Hello"); But was getting error.... Any suggestions... – TechTotie Feb 28 '14 at 12:24
  • @TechTotie: Yes, same as before: Read some tutorials on member function pointers. It should be `(evthndlr->*m_PtrToCapturer)("Hello")`. – Kerrek SB Feb 28 '14 at 16:25
0

This works,

struct method_ptr
{
    typedef void (T::*Function)(std::string&);
};

template <class T>
class EventHandler
{

private:
    typename method_ptr<T>::Function m_PtrToCapturer;
};
TechTotie
  • 127
  • 1
  • 15
-1

Since C++11, you can use using.

template <typename T>
using Function = void (T::*)(std::string);

(By the way, why is std::string called-by-value? I recommend const std::string &.)


Aha, I figured out your second question.

template <typename T>
method_ptr<T>::Function m_PtrToMemFunc; //<--

template is only applied to class and function (and cpp11's typedef&using...). you should write like this.

method_ptr<SomeClass>::Function m_PtrToMemFunc;
ikh
  • 10,119
  • 1
  • 31
  • 70
  • @Kerrek Beacuse I'm on phone, I can't... Even if my code has error, maybe it's just my mistype.. – ikh Feb 18 '14 at 19:20
  • 2
    Please don't post answers from a phone. That's the definition of "phoning it in". – Kerrek SB Feb 18 '14 at 19:21
  • @KerrekSB please elaborate on why this is bad. Some people don't have a `C++11` compiler and would want to know why the question is downvoted, for future reference. – Paweł Stawarz Feb 18 '14 at 19:23
  • @PawełStawarz: Half of it it flat-out wrong. The other half probably doesn't answer the question. – Kerrek SB Feb 18 '14 at 19:24
  • 1
    @ikh Your last edit is exactly what OP does. His issue is with using that pattern afterwards. But your usage example might be relevant. – François Moisan Feb 18 '14 at 19:28
  • @ikh: The first sentence is flat-out fabricated. *Please* verify this sort of stuff. You can even use an online compiler to do a basic smoke test. – Kerrek SB Feb 18 '14 at 19:36
  • Thanks Guys for your support. Further details on my requirements are specified in the above comment... – TechTotie Feb 18 '14 at 19:42
  • @KerrekSB I forgot WEB COMPILER!! and you're right, I had some error: template `typedef` can't be used to make member function pointer type, `using` is allowed. thank you so much! – ikh Feb 18 '14 at 19:53
  • @ikh Thanks for your answer. If I have templated class like template class EventHandler { struct method_ptr { typedef void (T::*Function)(std::string); }; private: method_ptr::Function m_PtrToCapturer; }; I still face errors saying error C2059: syntax error : '<' e:\project\EventHandler.h(13) : see reference to class template instantiation 'EventHandler' being compiled error C2238: unexpected token(s) preceding ';' Any idea where am I going wrong again – TechTotie Feb 19 '14 at 15:42
  • @TechTotie You need `typename method_ptr::Function m_PtrToCapturer;`. Also, make sure you have the include for method_ptr. – François Moisan Feb 19 '14 at 19:39