C++ 重载Iostream C++;

C++ 重载Iostream C++;,c++,overloading,iostream,C++,Overloading,Iostream,我正在编写一个只包含头的matrix3x3实现,我希望它是独立的,不依赖其他头,除了我也编写的vector3头 目前,我想让它超载ostream为什么不使用 例如: #include <iosfwd> class Example { public: Example(int i) : i(i) {} private: int i; friend std::ostream& operator<<(std::ostream& os, E

我正在编写一个只包含头的matrix3x3实现,我希望它是独立的,不依赖其他头,除了我也编写的vector3头

目前,我想让它超载ostream为什么不使用

例如:

#include <iosfwd>

class Example
{
public:
    Example(int i) : i(i) {}
private:
    int i;
    friend std::ostream& operator<<(std::ostream& os, Example const& example);
};

#include <iostream>

int main()
{
    Example e(123);
    std::cout << e << '\n';
}

std::ostream& operator<<(std::ostream& os, Example const& example)
{
    os << example.i;
    return os;
}
#包括
课例
{
公众:
示例(inti):i(i){}
私人:
int i;

friend std::ostream&Operator为
std::iostream
类使用转发声明?我可能不完全理解转发声明的含义,但这是否意味着matrix3x3头必须依赖iostream存在的事实?
不是存在的头。@PaulMcKenzie老实说,我可以看到w为什么有人会做出这样的假设。@Selvidian抱歉,我错过了你需要一个转发声明的
std::ostream
比如
template类std::basic\u ostream;使用std::ostream=std::basic\u ostream;
这仍然让我#在我的matrix3x3头中包含一些东西。我正在试图找到一个不需要我的问题的答案包括任何东西,我相信@user0042在评论中提出的建议会起到作用。@Selvidian:如果你不想“
”包括
一些东西,那么你使用的语言是错误的。
是为你的用例设计的。使用它。@Selvidian:what@user0042提出的建议一事无成(因为
做了完全相同的事情)并且可能会破坏某些东西(因为不允许自己转发声明标准类)。明白了,我的问题似乎已经得到了回答。谢谢