親子関係を持つようなクラスを作成したいと思い、次のようなクラスを書きました。
C++
1#include <vector> 2#include <memory> 3 4class Container { 5public: 6 std::vector<std::shared_ptr<Container>> children; 7 std::weak_ptr<Container> parent; 8 Container() { 9 } 10 ~Container() = default; 11 void addChild(std::shared_ptr<Container> child) { 12 children.push_back(child); 13 child->parent = this; // 怒られる 14 } 15};
しかし、std::shared_ptr<Container> child
の指している Container
の std::weak_ptr<Container> parent
に addChild
を呼び出した std::shared_ptr<Container>
を代入するつもりで書いた child->parent = this
で怒られてしまいます。this
は Container *
型だから怒られるのでしょうか?良く分かりません。助言をお願いします!!

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/11 03:11 編集
2016/10/11 03:23