我有一個類VectorSpace
與成員createVector()
它創建一個帶有指向VectorSpace
的共享指針的Vector
。這通過std::enable_shared_from_this
來實現。std :: enable_shared_from_this:沒有已知的從a到a的轉換
然而,這下面的代碼
#include <memory>
class Vector;
class VectorSpace;
class Vector {
public:
Vector(std::shared_ptr<VectorSpace> & space):
space_(space)
{
};
private:
std::shared_ptr<VectorSpace> space_;
};
class VectorSpace: std::enable_shared_from_this<VectorSpace> {
public:
VectorSpace(){};
Vector
createMember()
{
return Vector(shared_from_this());
};
};
int main() {
auto space = std::make_shared<VectorSpace>();
Vector x(space);
}
失敗與和非常奇怪的錯誤消息
test.cpp:8:3: note: no known conversion for argument 1 from ‘std::shared_ptr<VectorSpace>’ to ‘std::shared_ptr<VectorSpace>’
(這與GCC 4.9.2。)
編譯這裏的交易是什麼?
嘗試刪除&在向量構造函數:shared_from_this創建新的shared_ptr – Hcorg
@Hcorg剛剛注意到您的評論。你可能會考慮讓這類事情成爲答案 - 它解決了這個問題。 –
我可以告訴你編輯了錯誤信息,因爲你沒有顯示錯誤。您僅在錯誤中複製** note **。請不要隱藏錯誤消息。他們不應該是祕密! – Yakk