使用 ostream 作为参考 (C++)
我有一个家庭作业,其中头文件提供给我们,并且是不可更改的。我无法弄清楚如何正确使用“显示”功能,所以这里是相关代码。
头文件:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您所说,
display
需要一个std::ostream &
类型的参数。在您的显示方法实现中,您在
std::cout
中输出,这违背了接收输出流作为方法参数的逻辑。在这里,参数的要点是display
调用者将能够提供他选择的输出流。如果他的选择恰好是标准输出,他会写:这意味着您的
display
实现应该只在Out
参数中输出,而不是std::cout
。另请注意:
display
实现返回一个不应该返回的值(void
返回类型),std::
前缀为了清楚起见,我的回答是,但在您的情况下不需要它们,因为头文件包含using namespace std;
。As you said, the
display
expects a parameter of typestd::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 thedisplay
caller will be able to provide the output stream of his choice. If his choice happens to be the standard output, he will write :This means that your
display
implementation should only output in theOut
parameter and notstd::cout
.Also note that :
display
implementation returns a value, which it shouldn't (void
return type)std::
prefix in my answer for clarity, but they are not required in your case as the header file contains ausing namespace std;
.您需要做的是将所有使用过 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.
在您的显示方法中,您明确使用了 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
您没有传入 ostream 对象。将其更改为:
然后在您的类中将所有出现的 cout 替换为 Out。
此外,显示函数应返回 const ostream &而不是无效。您还应该使用 const ostream 引用而不是 ostream。
在类之外使用运算符是标准的:
这样您就可以执行以下操作:
You aren't passing in an ostream object. Change it to this:
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:
This way you can do things like: