【问题标题】:"unqualified-id" compiler error when accessing member of a class through a pointer通过指针访问类成员时出现“unqualified-id”编译器错误
【发布时间】:2015-01-06 02:31:54
【问题描述】:

我目前正在开发一个委托类,用于我的几个程序中。我的松散函数代码有效,但在绑定成员函数的代码中出现编译器错误。编译错误内容为

错误:在 '(' 标记之前应为 unqualified-id。

我看不出为什么会出现这种情况。我的代码,不包括编译良好的部分,如下:

template <typename T> class Delegate;

template <typename R, typename... Args>
class Delegate<R(Args)>
{
    typedef void* InstancePtr;   

//...

    template <typename C, R (C::*classFunction)(Args...)>
    static inline R MakeStubFunction(InstancePtr instance, Args... args)
    {
        //  vvv  error on this line  vvv
        return ( static_cast<C*>(instance)->(*classFunction) )(args...);
    }

//...

 };

谁能指出编译错误的来源?为了将来能够解决此类问题,“unqualified-id”是什么意思?

【问题讨论】:

  • "unqualified-id" 表示类似“某个名称”的意思,即字母,没有大括号等特殊字符,而是变量名称、类型名称、函数名称等。

标签: c++ c++11 void-pointers


【解决方案1】:

删除大括号并使用-&gt;* 运算符:

return ( static_cast<C*>(instance)->*classFunction )(args...);

【讨论】:

  • 为了缓解我的好奇心(如果你愿意的话)你知道为什么我使用的语法不起作用吗?看起来应该是有效的......
  • @Conduit 你不能在没有对象的情况下“取消引用”指向非静态成员的指针,但这就是你试图在大括号内做的事情。
  • 啊-这是因为函数的实际内容可能会根据实例中包含的成员变量而变化吗?
  • 如果是这样的话有点令人沮丧......我认为成员函数会根据成员变量与第一个地址使用的第一个地址的内存偏移量来“知道”它所需的成员变量的位置提供的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多