?
Solved

C++ overloading << operator using pointers

Posted on 2009-12-17
8
Medium Priority
?
463 Views
Last Modified: 2012-05-08
I am so confused. My assignment is to use the following in my header file. All I need to do is to write the defenition for it, but for now its not coming to me.

friend std::ostream& operator <<( std::ostream& outs, const Elevator * cup );
friend std::istream& operator >>( std::istream& ins, Elevator * cup );

My class contains only 3 private variables:
private:
        int myNumberOnboard;
        int myLimit;
        int myFloor;

I need to output those variables using the << operator.

The driver code that I need to use is:

Elevator * mine = new Elevator(10,10);
Elevator * yours = new Elevator(2,2);
mine->getLimit( );
mine->howManyOnboard( );
 
Elevator * junk = NULL;

// be careful&
cout << junk << endl;
cout << mine << endl;

cin >> junk;
// notice the difference
cout << junk << endl;

mine->setLimit( 1 );
/// notice the difference
cout << mine << endl;

delete (yours);
delete (mine);
delete (junk);

Open in new window

0
Comment
Question by:pzozulka
  • 4
  • 3
8 Comments
 
LVL 8

Author Comment

by:pzozulka
ID: 26073605
I tried writing a definition for << operator as such:

std::ostream& operator <<( std::ostream& outs, const Elevator * cup )
{
      outs << cup.myNumberOnboard;
      return outs;
}

But I get an error message when I try to build:
error C2228: left of '.myNumberOnboard' must have class/struct/union

But it does. cup is a pointer to an object/class which is "mine".
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 26073693
You are already on the right track. But: Since you are passing apointer, you need to use '->' to access it, i.e.
std::ostream& operator <<( std::ostream& outs, const Elevator * cup )
{
      outs << cup->myNumberOnboard;
      return outs;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 26073706
Oh, and you will have to declare the operator as a 'friend' to 'Elevator', e.g.
// fwd. declarations
class Elevator; 
std::ostream& operator <<( std::ostream&, const Elevator *);

// ...

class Elevator {

friend std::ostream& operator <<( std::ostream&, const Elevator *);

//...

};

//...

std::ostream& operator <<( std::ostream& outs, const Elevator * cup )
{
      outs << cup->myNumberOnboard;
      return outs;
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:pzozulka
ID: 26073772
So my definition looks like this now:

std::ostream& operator <<( std::ostream& outs, const Elevator * cup )
{
      outs << cup->myNumberOnboard << endl;
      outs << cup->myLimit << endl;
      outs << cup->myFloor << endl;
      return outs;
}

But, when I run the line below, it freaks out and wants to send a report to Microsoft.

Elevator * junk = NULL;
cout << junk << endl;
0
 
LVL 86

Expert Comment

by:jkr
ID: 26073831
You can't use a NULL piointer with this without checking for that condition - it should work if you use the following, though:
std::ostream& operator <<( std::ostream& outs, const Elevator * cup )
{
      if (!cup) return outs; // do nothing if it's a NULL pointer

      outs << cup->myNumberOnboard << endl;
      outs << cup->myLimit << endl;
      outs << cup->myFloor << endl;
      return outs;
}

Open in new window

0
 
LVL 8

Author Comment

by:pzozulka
ID: 26073920
Got it. OK thanks a lot. I appreciate that you didn't just give me the answers, but actually explained it. Thanks much.
0
 

Expert Comment

by:gianlucaesse
ID: 35871833
how about for the std::istream& operator >>( std::istream& ins, Elevator* & e)????
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.
Suggested Courses

807 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