重载运算符<<用于成员函数中类的枚举成员

发布于 2024-10-02 11:41:28 字数 664 浏览 8 评论 0 原文

我如何超载<<作为类成员的枚举的运算符。具体来说,我有以下代码:

#include <iostream>

using namespace std;

namespace foo {
    class bar {
    public:
        enum a { b, c, d};

        static void print() {
            cout << b << endl;
        }
    };

    ostream& operator<< (ostream& os, bar::a var) {

        switch (var) {
        case bar::b:
            return os << "b";
        case bar::c:
            return os << "c";
        case bar::d:
            return os << "d";
        }
        return os;
    }


}
int main() {
    foo::bar::print();

    return 0;
}

如何让打印函数打印“b”而不是“1”?

How do I overload the << operator for enums that are members of a class. Specifically, I have the following code below:

#include <iostream>

using namespace std;

namespace foo {
    class bar {
    public:
        enum a { b, c, d};

        static void print() {
            cout << b << endl;
        }
    };

    ostream& operator<< (ostream& os, bar::a var) {

        switch (var) {
        case bar::b:
            return os << "b";
        case bar::c:
            return os << "c";
        case bar::d:
            return os << "d";
        }
        return os;
    }


}
int main() {
    foo::bar::print();

    return 0;
}

How can I get the print function to print "b" instead of "1"?

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

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

发布评论

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

评论(3

恰似旧人归 2024-10-09 11:41:28

这是一个简单的解决方案:

#include <iostream>

using namespace std;

namespace foo {

    class bar {
    public:
        enum a { b, c, d};

        static void print();
    };

    ostream& operator<< (ostream& os, bar::a var) {

        switch (var) {
        case bar::b:
            return os << "b";
        case bar::c:
            return os << "c";
        case bar::d:
            return os << "d";
        }
        return os;
    }


    void bar::print() {
        cout << b << endl;
    }
}
int main() {
    foo::bar::print();

    return 0;
}

[编辑] 正如 aschepler 先前所述,您只需确保 operator<<(ostream &, bar::a)栏::打印

Here is a simple solution :

#include <iostream>

using namespace std;

namespace foo {

    class bar {
    public:
        enum a { b, c, d};

        static void print();
    };

    ostream& operator<< (ostream& os, bar::a var) {

        switch (var) {
        case bar::b:
            return os << "b";
        case bar::c:
            return os << "c";
        case bar::d:
            return os << "d";
        }
        return os;
    }


    void bar::print() {
        cout << b << endl;
    }
}
int main() {
    foo::bar::print();

    return 0;
}

[EDIT] As previously stated by aschepler, you only need to ensure that operator<<(ostream &, bar::a) is visible before the definition of bar::print.

意犹 2024-10-09 11:41:28
class bar {
public:
    enum a { b = 'b', c = 'c', d = 'd' };

    static void print() {
        cout << char(b) << endl;
    }
};
class bar {
public:
    enum a { b = 'b', c = 'c', d = 'd' };

    static void print() {
        cout << char(b) << endl;
    }
};
夜灵血窟げ 2024-10-09 11:41:28

问题是您使用 cout << bar:: 出现在您的 ostream<<之前 bar:: 重载已声明,因此它不会调用您的重载。将定义下移。

class bar {
public:
   enum a { b, c, d };
   static void print();
};

ostream& operator<<  (ostream& os, bar::a var) {
   ...

void bar::print()
{
   cout << b << endl;
}

编辑:我在输入此内容时看到其他人发布了该内容。

The problem is that your use of cout << bar:: comes before your ostream<< bar:: overload is declared, so it's not calling your overload. Move the definition down.

class bar {
public:
   enum a { b, c, d };
   static void print();
};

ostream& operator<<  (ostream& os, bar::a var) {
   ...

void bar::print()
{
   cout << b << endl;
}

EDIT: I see some else posted that while I was typing this.

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