From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25964 invoked by alias); 23 Feb 2009 16:14:07 -0000 Received: (qmail 25948 invoked by uid 22791); 23 Feb 2009 16:14:05 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail.gmx.net (HELO mail.gmx.net) (213.165.64.20) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Mon, 23 Feb 2009 16:13:58 +0000 Received: (qmail invoked by alias); 23 Feb 2009 16:13:55 -0000 Received: from NANO-66-21.grenoble.cnrs.fr (EHLO NANO-66-21.grenoble.cnrs.fr) [147.173.66.21] by mail.gmx.net (mp046) with SMTP; 23 Feb 2009 17:13:55 +0100 Date: Mon, 23 Feb 2009 16:14:00 -0000 From: Axel Freyn To: gcc-help@gcc.gnu.org Subject: Re: friendly operator overloading Message-ID: <20090223162016.GB30071@NANO-66-21.grenoble.cnrs.fr> References: <470cb4e0e9f0fb26fd9dac501dcf288d@prager.ws> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <470cb4e0e9f0fb26fd9dac501dcf288d@prager.ws> User-Agent: Mutt/1.5.18 (2008-05-17) X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2009-02/txt/msg00157.txt.bz2 Hi Bernd, for me (g++ 4.2.1) it runs in both situations. On Mon, Feb 23, 2009 at 08:09:22AM -0700, Bernd Prager wrote: > #include > #include > namespace xxx > { > class Container > { > friend std::ostream &operator<<(std::ostream&, const Container&); > private: > int id; > std::string s; > public: > Container() : s("Hello World") {} > }; > std::ostream &operator<<(std::ostream &stream, const Container &c) > { > return std::cout << c.s << std::endl; > } > } // namespace > using namespace xxx; > int main() > { > Container* c = new Container(); > std::cout << *c; > return 0; > } > > As long as I use it one (!) source file. > > When I separate the Container class in separate Container.h and a > Container.cpp files the compiler complains about: > $ g++ -march=pentium4 -mfpmath=sse tst.cpp container.cpp container.h > -o tst > container.h: In function ‘std::ostream& operator<<(std::ostream&, const > xxx::Container&)’: > container.h:13: error: ‘std::string xxx::Container::s’ is private > container.cpp:8: error: within this context > make: *** [tst] Error 1 I suspect this is a problem how you defined operator <<. It works with the following files ( operator << is defined OUTSIDE namespace xxx, and before it is used in class Container): container.h: namespace xxx{ class Container; } std::ostream &operator<<(std::ostream&, const xxx::Container&); namespace xxx { class Container { friend std::ostream &::operator<<(std::ostream&, const xxx::Container&); private: int id; std::string s; public: Container(); }; } container.cpp: #include "container.h" namespace xxx { Container::Container() : s("Hello World") {} } std::ostream &operator<<(std::ostream &stream, const xxx::Container &c) { return std::cout << c.s << std::endl; } test.cpp: #include "container.h" using namespace xxx; int main() { Container* c = new Container(); std::cout << *c; return 0; } Of course, it is also possible to define operator << inside the namespace. HTH - if not, it would be useful if you could send the three seperated files you used, Axel