Solved

operator<< overloading

Posted on 2003-11-16
5
283 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now