我想使用Python boost向Python公開一個C++的好友函數。C++中的朋友函數Python Boost
class Turtle{
friend const PV& GetHeading(const Turtle& t);
friend const PV& GetLeft(const Turtle& t);
friend const P& GetPoint(const Turtle& t);
friend void SetPoint(Turtle& t, const P& p);
public:
...
private:
PV h;
PV l;
};
在這裏,我已經包裝類PV和P所以沒有問題。我試圖包裝像普通功能一樣的朋友功能。像
BOOST_PYTHON_MODULE(TurtleWrapper)
{
class_<Turtle>("Turtle")
.def("GetHeading",&Turtle::GetHeading)
.def("GetLeft",&Turtle::GetLeft)
.add_property("h",&Turtle::GetHeading)
.add_property("l",&Turtle::GetLeft);
}
當我運行代碼時,我收到錯誤消息。
error: ‘GetHeading’ is not a member of ‘Turtle’
error: ‘GetLeft’ is not a member of ‘Turtle’
所以我認爲這是不宣友元函數和python提升的文件似乎不是辦法(或至少我沒有看到有關友元函數的說明)。任何幫助深表感謝。
你是否試圖在不需要的地方使用'friend'關鍵字? http://www.cplusplus.com/doc/tutorial/inheritance/ 將函數聲明爲'friend'通常意味着它不是你類的一部分,它是允許你的類的函數_outside_訪問private/protected成員。因此,錯誤是有效的。 – sneg
我的意思是C++代碼不是由我編寫的,它是更大代碼的一部分,這些函數確實被用作各種類的朋友,以便他們可以訪問這些類的數據成員。所以我不知道是否要包裝它們。但他們也被用在這個叫烏龜的班級裏。所以我的問題是它可以包裝到蟒蛇提升,如果是的話,我該怎麼做呢? – user665903