运算符 << c++ 中的过载

发布于 2024-09-16 20:23:38 字数 316 浏览 3 评论 0 原文

#include <iostream>
#include <fstream>

class obj
{
public:
 int i;
 friend ostream& operator<<(ostream& stream, obj o);
}

void main()
{
 obj o;
 ofstream fout("data.txt");
 fout<<o;
 fout.close();
}

这是我的代码,出现错误。 错误:ostream:不明确的符号。

任何人都可以帮助我。

#include <iostream>
#include <fstream>

class obj
{
public:
 int i;
 friend ostream& operator<<(ostream& stream, obj o);
}

void main()
{
 obj o;
 ofstream fout("data.txt");
 fout<<o;
 fout.close();
}

This is the my code, am getting error.
error : ostream : ambiguous symbol.

any one can help me.

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

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

发布评论

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

评论(6

半透明的墙 2024-09-23 20:23:38

您需要指定名称空间。在 ostream 前面加上 std - 即 std::ostream

另外,您应该通过 const 引用将 obj 类型传递给运算符:

friend ostream& operator<<(ostream& stream, const obj& o);

You need to specify the namespace. Prefix ostream with std - i.e. std::ostream

Also, you should pass the obj type by const reference to the operator:

friend ostream& operator<<(ostream& stream, const obj& o);
瀞厅☆埖开 2024-09-23 20:23:38

你没有使用命名空间std(反正使用命名空间std是习惯),所以编译器不知道ostream到底是什么。除此之外,你实际上没有定义operator<<,只是声明了它,所以即使它认识到了,它也不知道该怎么做,因为你没有告诉它。

You didn't use namespace std (using namespace std is habit anyway) so the compiler doesn't know what on earth an ostream is.In addition to that, you didn't actually define operator<<, only declared it, so even if it recognizes it, it won't know what to do since you didn't tell it.

夏雨凉 2024-09-23 20:23:38

据我所知,您需要

  • 添加

    使用 std::ostream;
    using std::ofstream;

  • 在类声明之后添加 ;
  • 为 << 提供一个实现。操作员。

最后你应该得到类似的结果:

#include <iostream>
#include <fstream>

using std::ostream;
using std::ofstream;

class obj
{
public:
 int i;
 friend ostream& operator<<(ostream& stream, const obj& o);
};

ostream& operator<<(ostream& stream, const obj& o)
{
  std::cout << o.i;
  return stream;
}

int main()
{
  obj o;
  ofstream fout("data.txt");
  fout << o;
  fout.close();
}

As I see it you need to

  • Add

    using std::ostream;
    using std::ofstream;

  • Add a ; after the class declaration
  • Povide an implementation for the << operator.

In the end you should end up with something like:

#include <iostream>
#include <fstream>

using std::ostream;
using std::ofstream;

class obj
{
public:
 int i;
 friend ostream& operator<<(ostream& stream, const obj& o);
};

ostream& operator<<(ostream& stream, const obj& o)
{
  std::cout << o.i;
  return stream;
}

int main()
{
  obj o;
  ofstream fout("data.txt");
  fout << o;
  fout.close();
}
甲如呢乙后呢 2024-09-23 20:23:38

ofstream 位于命名空间 std 中,因此您需要像这样声明 fout

std::ofstream fout("data.txt");

我假设您只是省略了运算符的定义<< ;函数为了简单起见。显然,您需要编写该函数的主体以便下一行进行编译。

ofstream is in namespace std, so you need to declare fout like this:

std::ofstream fout("data.txt");

I'll assume you simply omitted the definition of your operator<< function for simplicity. Obviously, you'll need to write the body of that function for your next line to compile.

ζ澈沫 2024-09-23 20:23:38

ostream 是 std:: 命名空间的成员,因此可以在类声明之前放置 using namespace std; ,或者使用 std::ostream 显式引用它。

ostream is a member of the std:: namespace, so either put a using namespace std; before your class declaration or explicitly refer to it with std::ostream.

不离久伴 2024-09-23 20:23:38

考虑将对象作为引用传递,否则每次都会通过复制构造函数创建一个新的 obj 对象。

friend ostream& operator<<(ostream& stream, obj& o);  

Consider passing your object in as a reference otherwise a new obj object will be created each time via the copy constructor.

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