[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

operator<< overloading

I have two classes that I need to overload the stream operator << on. One works, the other doesn't, but I'm having trouble seeing why the latter doesn't work.

Here they are. I'm hoping someone can help me point out the difference.

/////////////////////////////////////////////////////////////////////////////
// this class overloads << just fine.
class Location {
public:
    Location(Country country, std::string& location_name);
    Country country() const { return myCountry; }
    const std::string& locationName() const { return myLocationName; }
    float longDistanceRateFromCanada() const;

private:
    Country myCountry;
    std::string myLocationName;
};

static std::ostream& operator<<(std::ostream& os, const Location& location) {
    os << location.locationName();
    return os;
}
////////////////////////////////////////////////////////////////
// this class does not

class PhoneNumber {
private:
    unsigned long myPhoneNumber;
public:
    PhoneNumber(unsigned long p) { myPhoneNumber = p; }
    int areaCode();
    unsigned long getNumber();
};

static std::ostream& operator<<(std::ostream& os, const PhoneNumber& pn) {
    os << pn.getNumber();
    return os;
}
///////////////////////////////////////////////////////////

Thanks for any help you can provide.




0
seagu11
Asked:
seagu11
  • 3
  • 2
1 Solution
 
AlexFMCommented:
What doesn't work exactly? Not compiled, not linked, doesn't print anything, prints junk etc.
0
 
AlexFMCommented:
My guess is: make getNumber function const:

unsigned long getNumber() const;

You can also write it's implementation.
0
 
seagu11Author Commented:
I apologize for not including the error. I totally meant to.

In file included from PhoneNumber.cpp:1:
PhoneNumber.h: In function `class ostream & operator <<(ostream &, const PhoneNumber &)':
PhoneNumber.h:16: passing `const PhoneNumber' as `this' argument of `long unsigned int PhoneNumber::getNumber()' discards qualifiers
*** Error code 1
0
 
seagu11Author Commented:
Thanks AlexFM. Making the getNumber() function const did the trick.
it's implementation was simply

unsigned long PhoneNumber::getNumber() const{
    return myPhoneNumber;
}

Now I need to find out *why* that worked so I don't do it again.
0
 
AlexFMCommented:
static std::ostream& operator<<(std::ostream& os, const PhoneNumber& pn)

This declares pn as const, so you cannot call non-const PhoneNumber members. Marking getNumber const (and it is really const because it doesn't change class members) makes C++ compiler happy.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now