我第一次尝试使用共享指针,但是我遇到了一个问题,我不能从指针访问Element的成员函数。我已经查看了网上的示例,似乎可以访问Element的函数,但我怀疑在设置指针时出了问题,但我无法弄清楚问题出在哪里。
如何从shared_ptr中访问Element的公共成员函数?(我使用的是Xcode 5.1.1)
#include <memory>
#include <iostream>
class Element
{
private:
std::string name;
std::shared_ptr<Element> * firstChild;
std::shared_ptr<Element> * lastChild;
std::shared_ptr<Element> * nextSibling;
public:
void addChild(std::shared_ptr<Element> * child)
{
if (lastChild != nullptr) {
lastChild->setNextSibling(child); //Error: No member named 'setNextSibling' in 'std::__1::shared_ptr<Element>'
lastChild = child;
}
else {
firstChild = lastChild = child;
}
}
void setNextSibling(std::shared_ptr<Element>* p)
{
nextSibling = p;
}
};