forward declaration of std::ostream

huangapple go评论55阅读模式
英文:

forward declaration of std::ostream

问题

以下是您要翻译的内容:

The files are organized as following:

//MyClass.h
#pragma once
namespace std {
	class ostream;
};
class MyClass
{
private:
	int a;
public:
	friend std::ostream& operator<<(std::ostream& o, const MyClass& obj);
};
//MyClass.cpp
#include<iostream>
std::ostream& operator<<(std::ostream& o, const MyClass& obj)
{
    o << obj.a << std::endl;
    return o;
}
//main.cpp
#include "MyClass.h"
#include<iostream>

int main()
{
	MyClass obj;
	std::cout << obj;
}

It failed compiling with errors on overloading operator<<:

Error C2371 'std::ostream': redefinition; different basic types

What's the issue here and how to correct it?

英文:

The files are organized as following:

//MyClass.h
#pragma once
namespace std {
	class ostream;
};
class MyClass
{
private:
	int a;
public:
	friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; o, const MyClass&amp; obj);
};
//MyClass.cpp
#include&lt;iostream&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; o, const MyClass&amp; obj)
{
    o &lt;&lt; obj.a &lt;&lt; std::endl;
    return o;
}
//main.cpp
#include&quot;MyClass.h&quot;
#include&lt;iostream&gt;

int main()
{
	MyClass obj;
	std::cout &lt;&lt; obj;
}

It failed compiling with errors on overloading operator&lt;&lt;:

> Error C2371 'std::ostream': redefinition; different basic types

What's the issue here and how to correct it?

答案1

得分: 4

如其他人所指出,“ostream”不是一个类,而是一个模板实例的别名,基本上是“ostream = basic_ostream”。这是标准库的实现细节;它存在是因为它还需要支持“wostream”(即“basic_ostream<wchar_t>”)和可能的其他流类型。但是其他实现可能会以不同的方式(例如使用源代码生成)或非常不同的方式(分发预编译模块)来完成这个任务。您不应依赖于任何特定的实现。


您不应在“std”命名空间内声明任何内容(除非C++标准明确允许)。要进行前向声明“ostream”等内容,请在您的头文件中使用#include <iosfwd>

或者,只需使用#include <iostream>,而不是前向声明 — 记住的技术细节较少,编译略慢一些 — 您可以根据自己的判断决定哪种更好。

英文:

As other people noted, ostream is not a class but an alias to a template instantiation, basically ostream = basic_ostream&lt;char&gt;. This is an implementation detail of the standard library; it's there because it needs to support also wostream (which is basic_ostream&lt;wchar_t&gt;) and possibly other stream types. But other implementations may do it differently (using e.g. source code generation) or very differently (distributing pre-compiled modules). You should not depend on any particular implementation.


You should not declare anything inside the std namespace (except stuff which the C++ Standard explicitly permits). To forward-declare ostream and such, use #include &lt;iosfwd&gt; in your header file.

Alternatively, just do #include &lt;iostream&gt; instead of forward declaration — less technical details to remember vs slightly slower compilation — do your own judgment on what is better.

huangapple
  • 本文由 发表于 2023年7月23日 20:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定