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

标签 c++ multithreading iso

我正在尝试以下代码:

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

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

我收到警告:

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

关于c++ - 错误 : ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819713/

相关文章:

c++ - 我可以使用我的部分数据作为锁吗?

c++ - SDL:体系结构 x86_64 的 undefined symbol

multithreading - 如果在MVar/Chan/TQueue上无限期阻塞,Haskell线程是否会被GC?

format - MP4 文件格式规范

c++ ->4D 阵列 MatLab 的巨大性能损失的原因?

c++ - 在类中使用私有(private)变量

multithreading - JBoss EAP中的JSR-236并发实用程序支持

java - 想要并行运行非线程安全库 - 可以使用多个类加载器来完成吗?

c++ - Clang 和 Gcc 不同意实例化后的显式特化

java - 将 ISO3 国家/地区代码转换为整个国家/地区名称,Android