标签: stream-operators

在gdb中调用operator <<

你如何operator<<(std::ostream &os, const ClassX &x)从gdb内部打电话?

换句话说,如何在gdb中打印对象?

call std::cout<<x或者call operator<<(std::cout, x)似乎不适合我!

有任何想法吗?

c++ gdb stream-operators

16
推荐指数
2
解决办法
6417
查看次数

为我的班级重载流操作符<< >>的正确方法是什么?

我对如何在C++中为我的类重载流操作符感到困惑,因为它似乎是流类上的函数,而不是我的类.这样做的正常方法是什么?目前,对于"来自"运营商,我有一个定义

istream& operator>>(istream& is, Thing& thing) { // etc...
Run Code Online (Sandbox Code Playgroud)

哪个有效.在Thing类的定义中没有提到它.我希望它能够在其实现中访问我的Thing类的成员 - 我该怎么做?

c++ operator-overloading stream-operators

5
推荐指数
3
解决办法
4346
查看次数

使用 basic_ostream 重载运算符 &lt;&lt;

为什么使用用户定义的类进行流操作的典型标头C通常如下所示:

std::ostream& operator<<(std::ostream& os, const C& c);
std::istream& operator>>(std::istream& is, C&);
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

template <class CharT, class Traits> 
std::basic_ostream<CharT, Traits>& operator<<(
        std::basic_ostream<CharT, Traits>& os
        const C& c);

template <class CharT, class Traits> 
std::basic_istream<CharT, Traits>& operator>>(
        std::basic_istream<CharT, Traits>& is
        C& c);
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么流运算符的通常重载是用 完成的,它是ofstd::ostream的 typedef ,为什么不直接用 完成?charstd::basic_ostreamstd::basic_ostream

例如:

class C
{
    ...
};

std::ostream& operator<<(std::ostream& os, const C& c)
{
    ...
}

int main()
{
    C c;
    std::wofstream myFile("myFile.txt");
    myFile << c; //Impossible
}
Run Code Online (Sandbox Code Playgroud)

这里写operator<< …

c++ templates operator-overloading std stream-operators

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

如何为 boost log 和自定义类型定义输出流运算符

我能够为一个简单的结构定义输出流运算符,但不能为 std::array 定义输出流运算符。以下代码无法编译。出了什么问题,我该如何解决?

\n\n
#include <array>\n#include <iostream>\n#include <boost/log/core.hpp>\n#include <boost/log/trivial.hpp>\n\nusing hash_t = std::array< unsigned char, 32 >;\n\nstd::ostream& operator<< ( std::ostream& os, hash_t const& arr )\n{\n    os << "ole!";\n    return os;\n}\n\nint main(int, char*[])\n{\n    hash_t arr;\n    std::cerr << arr << std::endl; // complies cleanly\n    BOOST_LOG_TRIVIAL(debug) << arr; // Error\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误就来了。

\n\n

GCC(增强 1.55,gcc-4.9.2):

\n\n
In file included from /usr/include/boost/log/sources/record_ostream.hpp:31:0,\n                 from /usr/include/boost/log/trivial.hpp:23,\n                 from trival.cpp:4:\n/usr/include/boost/log/utility/formatting_ostream.hpp: In instantiation of \xe2\x80\x98boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>& boost::log::v2s_mt_posix::operator<<(boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>&, const T&) [with CharT = char; TraitsT = …
Run Code Online (Sandbox Code Playgroud)

c++ templates stream-operators boost-log

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

流嵌入式命名空间中的运算符重载

最近,当我实现一个类时,我创建了一个名为运算符的嵌套命名空间,我在其中添加了流操作符.

我之所以这样做是因为我经常需要在名称命名空间我以外的命名空间中使用它们

using my_namespace::operators;
Run Code Online (Sandbox Code Playgroud)

就在我想要的地方,就是这样.

这里我有一个类Point,一个类Segment及其流运算符的示例,其中Segment的流调用Point的流.但是......我无法编译:

分数点:

#ifndef POINT_HPP
#define POINT_HPP

#include <iostream>

namespace geom {

class Point
{
public:
    Point(int x_, int y_) : x(x_), y(y_) {};
    int x;
    int y;
 };

namespace operators {
    std::ostream& operator<<(std::ostream& out, const Point& p)
    {
        out << "(" << p.x << ", " << p.y << ")";
        return out;
    }
} // ~ namespace geom::operators
} // ~ namespace geom

#endif // ~ POINT_HPP
Run Code Online (Sandbox Code Playgroud)

类别细分:

#ifndef SEGMENT_HPP
#define SEGMENT_HPP

#include …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces operator-overloading stream-operators

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