Using ostream as a reference (C++)
Asked Answered
D

4

11

I have a homework assignment where the header file is provided to us, and is unchangeable. Im having trouble figuring out how to correctly use a "display" function, so here is the relevant code.

The header file:

#ifndef SET_
#define SET_

typedef int EType;

using namespace std;

#include <iostream>

class Set
{
  private:

    struct Node
    {
      EType Item;     // User data item
      Node * Succ;    // Link to the node's successor
    };

    unsigned Num;     // Number of user data items in the set
    Node * Head;      // Link to the head of the chain

  public:

    // Various functions performed on the set

    // Display the contents of the set
    //
    void display( ostream& ) const;

};

#endif

Here is my implementation of the function "display":

void Set::display( ostream& Out ) const
{
  Node * temp = Head;
  cout << "{ ";
  while( temp != NULL )
  {
  cout << temp << ", ";
  temp = temp->Succ;
  return Out;
  }
}

And here is my driver:

#include <iostream>
#include <iomanip>
#include "/user/cse232/Projects/project08.set.h"

using namespace std;

int main()
{
  Set X;
  X.insert(10);
  X.insert(20);
  X.insert(30);
  X.insert(40);
  X.display();
}

The error I am receiving says that in my driver, I am not using the correct parameters. I understand this because the .h file uses ostream& as a parameter. My question is, what do I use in my driver file when calling "display" as a good parameter?

Disport answered 18/11, 2010 at 16:26 Comment(1)
That's a newbie question in pretty good shape. All irrelevant bits removed, yet (almost) all relevant bits shown. Next time, just add the exact error message and indicate the exact line it points to, and it'd be perfect.Prut
Y
11

As you said, the display expects a parameter of type std::ostream &.

In your display method implementation, you are outputting in std::cout which defies the logic of receiving the output stream as a parameter to the method. Here, the point of the parameter is that the display caller will be able to provide the output stream of his choice. If his choice happens to be the standard output, he will write :

x.display(std::cout);

This means that your display implementation should only output in the Out parameter and not std::cout.

Also note that :

  • Your display implementation returns a value, which it shouldn't (void return type)
  • I use the std:: prefix in my answer for clarity, but they are not required in your case as the header file contains a using namespace std;.
Yasmineyasu answered 18/11, 2010 at 16:30 Comment(2)
Oh, thanks for the explanation. I changed my implementation file so that instead of using cout << blahblah, it uses Out << blahblah. I also removed the "return Out;" line and used std::cout as a parameter, and the program finally compiled and gave me output! The output is addresses, but that is an easy fix, I think. Thank you for your help! :)Disport
+1 - using the parameter like this is preferable as this allows your class to support output to any other C++ standard output stream, (e.g. ostringstream, ofstream) without modificationBlackness
D
0

What you need to do is substitute out for all the places you have used cout. Also pass cout as a parameter like x.display(cout). This is because, cout is off type ostream and all this initialization is done in iostream.

Donegal answered 18/11, 2010 at 16:31 Comment(0)
L
0

In your display method, you are explicitly using cout. But this is the "standard out". The method should rather use Out. So in display(), just replace every occurrence of cout with Out.

Then use display( cout ); in your call

Lineman answered 18/11, 2010 at 16:31 Comment(0)
K
0

You aren't passing in an ostream object. Change it to this:

X.display(cout);

Then in your class replace all occurrences of cout with Out. Also, the display function should return a const ostream & instead of void. You should also be using const ostream references instead of ostream.

It is standard to use an operator outside of the class:

const ostream & operator<< (const ostream & Out, const Set & set)
{
  // display your Set here using out, not cout
  return out;
}

This way you can do things like:

cout << "This is my set: " << mySet << endl;
Khachaturian answered 18/11, 2010 at 16:31 Comment(2)
I don't think I'd have a problem writing it that way. I feel like a lot of my projects would be easier if I could write my own header files, but my professor includes them with the project assignments, so I don't have a choice but to do it the other way. :( However, the part about using Out instead of cout was something I hadn't thought of, so thank you for the answer!Disport
FYI: The above code doesn't compile with any compilers on my system (Linux: gcc or icc) once you start actually trying to write to the ostream. The gcc implementation of ostream does not provide "const" versions of operator<< for basic types, so you need to use a non-const ostream& object.Downstate

© 2022 - 2024 — McMap. All rights reserved.