小编Ica*_*rus的帖子

在另一个类中的另一个 << 重载中使用重载的 << 运算符

所以问题来了。我有一个 B 类,其中我重载了 << 运算符,还有一个 A 类,其中 << 运算符也重载了。但是,类 B 中的 << 重载似乎在类 A 中的 << 重载中不起作用。它只是返回 b 的地址,就好像类 B 中的 << 重载不存在一样。

任何帮助将不胜感激

#pragma once
#include <ostream>

using namespace std;

class B {
public:

    B(int x) {
        this->x = x;
    }
    
    friend ostream& operator<<(ostream& os, B& b)
    {
        os << "this is B " << b.x;
        return os; 
    }

private:
    int x;
};
Run Code Online (Sandbox Code Playgroud)
#pragma once
#include <ostream>
#include "B.h"

using namespace std;

class A {
public:

    A(B* b) { …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading operator-keyword

1
推荐指数
1
解决办法
38
查看次数