模板函数C ++中的指针 [英] Templated Function Pointer in C++

查看:185
本文介绍了模板函数C ++中的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着模板成员函数指针的问题。
代码如下所示。

  #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):错误C2146:语法错误: before identifier'm_PtrToCapturer'



我面对这个错误。



即使我使用

  method_ptr< EventHandler> :: Function m_PtrToCapturer; 

作为成员变量我得到与上面相同的错误。

method_ptr< T> :: Function 是一个依赖名称(依赖于 T ),您需要使用 typename
pre> template< class T>
class EventHandler
{
private:
typename method_ptr< T> :: Function m_PtrToCapturer;
// ^^^^^^^^
};


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.

解决方案

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;
//  ^^^^^^^^
};

这篇关于模板函数C ++中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆