2016-11-26 40 views
2

我試圖以下代碼:錯誤:ISO C++禁止服用不合格或括號的非靜態成員函數的地址,以形成一個指向成員函數

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

其中發送者是的一個成員函數從上面的行被調用的方法的同一類。

我得到警告:

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編譯代碼。

回答

5

錯誤消息非常明確。

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

+0

是的,你的代碼似乎工作。雖然是C++的新手,但我不明白爲什麼。我的意思是,你和我的區別只在於你的Transmitter :: sender周圍沒有括號。 – AbbasFaisal

+0

@AbbasFaisal由於ISO C++這麼說,請參閱我的編輯 – Danh

相關問題