加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用ostream作为参考(C)

发布时间:2020-12-16 03:07:05 所属栏目:百科 来源:网络整理
导读:我有一个家庭作业,头文件提供给我们,是不可改变的.我无法弄清楚如何正确使用“显示”功能,所以这里是相关的代码. 头文件: #ifndef SET_#define SET_typedef int EType;using namespace std;#include iostreamclass Set{ private: struct Node { EType Item;
我有一个家庭作业,头文件提供给我们,是不可改变的.我无法弄清楚如何正确使用“显示”功能,所以这里是相关的代码.

头文件:

#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

这是我的功能实现“显示”:

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&作为参数.我的问题是,在调用“显示”作为一个很好的参数时,我在驱动程序文件中使用什么?

解决方法

正如你所说,显示器需要一个类型为std :: ostream& amp;

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

x.display(std::cout);

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

另请注意:

>你的显示实现返回一个值,它不应该(void return type)>为了清楚起见,我在我的答案中使用std ::前缀,但是在你的情况下它们不是必需的,因为头文件包含一个using namespace std;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读