Solved

operator<< overloading

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

17 Experts available now in Live!

Get 1:1 Help Now