c++ - Friend function not working -
in following code trying have friend function access private member of class. understanding correctly declaring friend function, vs2012 giving me error:
error c2248: 'x::s::s_' : cannot access private member declared in class 'x::s'
can suggest doing wrong? simplest example demonstrates compiler error come with.
namespace x { class s { friend std::string r(x::s &s); std::unique_ptr<std::istream> s_; }; } std::string r(x::s &s) { auto& x = s.s_; return ""; }
you're defining ::r
, not x::r
, friend declaration for. move function namespace alongside class, or define right inside class, though can problematic class template or keeping class definition concise. if definition in separate file, can still enclose namespace class add namespace. suggest removing x::
qualification it's in x
.
namespace x { class s { friend std::string r(s &s); std::unique_ptr<std::istream> s_; }; std::string r(s &s) { auto& x = s.s_; return ""; } }
Comments
Post a Comment