使用 ostream 作为参考 (C++)

发布于 2024-10-03 13:22:20 字数 1258 浏览 1 评论 0原文

我有一个家庭作业,其中头文件提供给我们,并且是不可更改的。我无法弄清楚如何正确使用“显示”功能,所以这里是相关代码。

头文件:

#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

这是我对函数“display”的实现:

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

这是我的驱动程序:

#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();
}

我收到的错误表明在我的驱动程序中,我没有使用正确的参数。我理解这一点是因为 .h 文件使用 ostream&作为参数。我的问题是,当调用“display”作为一个好的参数时,我在驱动程序文件中使用什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栩栩如生 2024-10-10 13:22:20

正如您所说,display 需要一个 std::ostream & 类型的参数。

在您的显示方法实现中,您在 std::cout 中输出,这违背了接收输出流作为方法参数的逻辑。在这里,参数的要点是 display 调用者将能够提供他选择的输出流。如果他的选择恰好是标准输出,他会写:

x.display(std::cout);

这意味着您的 display 实现应该只在 Out 参数中输出,而不是 std::cout

另请注意:

  • 您的 display 实现返回一个不应该返回的值(void 返回类型),
  • 我在中使用 std:: 前缀为了清楚起见,我的回答是,但在您的情况下不需要它们,因为头文件包含 using namespace std;

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;.
何止钟意 2024-10-10 13:22:20

您需要做的是将所有使用过 cout 的地方替换为 out 。还可以将 cout 作为参数传递,如 x.display(cout)。这是因为,cout 不是 ostream 类型,所有这些初始化都是在 iostream 中完成的。

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.

高速公鹿 2024-10-10 13:22:20

在您的显示方法中,您明确使用了 cout。但这是“标准输出”。该方法应该使用 Out。因此,在 display() 中,只需将出现的所有 cout 替换为 Out 即可。

然后使用显示(cout);
在你的通话中

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

怕倦 2024-10-10 13:22:20

您没有传入 ostream 对象。将其更改为:

X.display(cout);

然后在您的类中将所有出现的 cout 替换为 Out。
此外,显示函数应返回 const ostream &而不是无效。您还应该使用 const ostream 引用而不是 ostream。

在类之外使用运算符是标准的:

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

这样您就可以执行以下操作:

cout << "This is my set: " << mySet << endl;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文