Solved

operator<< overloading

Posted on 2003-11-16
5
294 Views
Last Modified: 2010-04-01
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
Comment
Question by:seagu11
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9758488
What doesn't work exactly? Not compiled, not linked, doesn't print anything, prints junk etc.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 9758490
My guess is: make getNumber function const:

unsigned long getNumber() const;

You can also write it's implementation.
0
 

Author Comment

by:seagu11
ID: 9758548
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
 

Author Comment

by:seagu11
ID: 9758554
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 9758581
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question