Article View: alt.comp.lang.learn.c-c++
Article #831Re: operator<< and namespace
From: Thomas Maeder
Date: Wed, 19 Oct 2005 15:35
Date: Wed, 19 Oct 2005 15:35
61 lines
2451 bytes
2451 bytes
"tengereche" <jalkadir@gosonic.ca> writes:
> This program does compile, but the linker says:
> main.o(.text+0x1a4):main.cpp: undefined reference to
> `jme::operator<<(std::ostream&, jme::Name const&)'
>
> here is the program's snips.
>
> --------- strtools.hpp
> namespace{
Are you sure that this isn't
namespace jme {
?
> calss strtools{
> std::string str;
> ........
>
> };
> }
>
> --------- name.hpp
> namespace{
Are you sure that this isn't
namespace jme {
?
Please always copy&paste code. Don't re-type.
> class Name : public jme::strtools{
> ....
> // This only gives you an idea as to what the f'tions do
> const std::string& getNameStr() const{return str;}
> void setName( const std::string& x){str = x;}
> void setName( const char* x){str = x;}
>
> friend std::ostream& operator<<( std::ostream& os,
> const jme::Name& obj );
> friend std::istream& operator>>( std::istream& is,
> jme::Name& obj );
>
> };
> }
>
> --------- name.cpp
> std::ostream& operator<<( std::ostream& os, const jme::Name& obj ) {
> return os << obj.getNameStr(); }
> std::istream& operator>>( std::istream& is, jme::Name& obj ) {
> return is >> obj.str;
>
> }
These operators belong to the global namespace. Define them in
namespace jme.
Technical Details
Message-ID:
<m27jc94izy.fsf@madbox2.local>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!news.hispeed.ch!not-for-mailReferences:
<1129731313.976379.198160@g49g2000cwa.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 114592,beb845dd377bccf0 X-Google-Thread: 10ba33,beb845dd377bccf0 X-Google-Attributes: gid114592,gid10ba33,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!news.hispeed.ch!not-for-mail From: Thomas Maeder <maeder@glue.ch> Newsgroups: alt.comp.lang.learn.c-c++,gnu.g++.help Subject: Re: operator<< and namespace Date: Wed, 19 Oct 2005 17:35:45 +0200 Organization: Cablecom Newsserver Lines: 61 Message-ID: <m27jc94izy.fsf@madbox2.local> References: <1129731313.976379.198160@g49g2000cwa.googlegroups.com> NNTP-Posting-Host: 84-73-152-93.dclient.hispeed.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.hispeed.ch 1129736188 8983 84.73.152.93 (19 Oct 2005 15:36:28 GMT) X-Complaints-To: news@hispeed.ch NNTP-Posting-Date: Wed, 19 Oct 2005 15:36:28 +0000 (UTC) User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux) Cancel-Lock: sha1:Z5nId3O6nD+CtsPvPHccDJWeEtE=