help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: operator<< and namespace


From: Mark P
Subject: Re: operator<< and namespace
Date: Wed, 19 Oct 2005 21:55:59 GMT
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041206 Thunderbird/1.0 Mnenhy/0.7.2.0

Thomas Maeder wrote:
"Al-Burak" <jalqadir@netscape.net> writes:


Correction
I accidentally forgot to add the name of the namespace


As suspected.



--------- 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;
}


This is your problem. As defined here, these operators belong to the
global namespace. But you want them to belong to the namespace jme:

namespace jme
{
  std::ostream &operator<<(std::ostream &os, Name const &obj)
  {
    return os << obj.getNameStr();
  }

  std::istream &operator>>(std::istream &is, Name &obj)
  {
    return is >> obj.str;
  }
}

and you should be fine.

Am I mistaken in thinking that the functions need to be declared in a header included by main.cpp? I see a "friend" declaration in name.hpp and a definition in name.cpp, but I thought a friend declaration alone did not imply a function declaration.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]