error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

我正在尝试以下代码:

std::thread t(&(Transmitter::sender), this, some_variables);

其中 sender 是同一 class 的成员函数,从其方法调用上面的行。

我收到警告:

Transmitter.h: In member function 'int Transmitter::transmit_streams(std::vector<std::vector<single_stream_record> >, int, Receiver&)':
Transmitter.h:81:44: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&Transmitter::sender' [-fpermissive]

尽管它编译并运行良好。我怎样才能删除这个警告。

我的 g++ 是 4.6.3,我用 -std=c++0x 编译代码。

报错信息很清楚。

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&Transmitter::sender' [-fpermissive]

来自expr.unary.op

A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [ Note: That is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type “pointer to member”. Neither does qualified-id, because there is no implicit conversion from a qualified-id for a non-static member function to the type “pointer to member function” as there is from an lvalue of function type to the type “pointer to function” ([conv.func]). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id's class. — end note ]

您需要使用:

    std::thread t(&Transmitter::sender, this, some_variables);

this demo