Tag: ostream

为什么std :: ostream和char之间的operator <<函数是非成员函数?

当我运行下面的程序 #include <iostream> int main() { char c = 'a'; std::cout << c << std::endl; std::cout.operator<<(c) << std::endl; return 0; } 我得到了输出 a 97 进一步挖掘在http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt ,我注意到, std::ostream::operator<<()没有一个具有char作为参数types的重载。 函数调用std::cout.operator<<(a)被parsing为std::ostream::operator<<(int) ,它解释了输出。 我假设std::ostream和char之间的operator<<函数在其他地方被声明为: std::ostream& operator<<(std::ostream& out, char c); 否则, std::cout << a将parsing为std::ostream::operator<<(int) 。 我的问题是为什么声明/定义为非成员函数? 有没有已知的问题阻止它成为一个成员函数?

C ++或库中是否存在std :: ostream实现?

我正在寻找一个像/dev/null一样的std::ostream实现。 它会忽略任何stream式传输给它的东西。 标准库或Boost中是否存在这样的事情? 或者我必须推出自己的?

我如何创build我自己的ostream / streambuf?

为了教育目的,我想创build一个ostream和stream缓冲区来做: 修正endians当做myVar; 存储在deque容器中,而不是使用std:cout或写入文件 logging额外的数据,比如我做了多less次,我做了多less次.write,我写的字节数和flush()的次数。 但是我不需要所有的信息。 我尝试超载,但失败了。 我试图通过做超载写 ostream& write( const char* s, streamsize n ) 在我的basic_stringstream2类(我复制粘贴basic_stringstream到我的cpp文件并修改它),但代码保持使用basic_ostream。 我查看了代码,它看起来像我需要重载xsputn(这是没有提到这个网页上http://www.cplusplus.com/reference/iostream/ostream ),但还有什么我需要超载? 以及如何构build我的类(它需要inheritance等)?

如何在c ++中使用ostream打印无符号字符为hex?

我想在C ++中使用无符号的8位variables。 无论是unsigned char还是uint8_t都可以完成算术运算(这是预期的,因为AFAIK uint8_t只是unsigned char的别名,或者是debugging器提供的。 问题是,如果我在C ++中使用ostream打印出variables,将它视为char。 如果我有: unsigned char a = 0; unsigned char b = 0xff; cout << "a is " << hex << a <<"; b is " << hex << b << endl; 那么输出结果是: a is ^@; b is 377 代替 a is 0; b is ff 我尝试过使用uint8_t ,但正如我之前提到的那样,它是unsigned char的typedef,所以它也是这样。 […]

为模板类重载friend operator <<

我已经阅读了几个有关我的问题在stackoverflow现在,似乎没有解决我的问题。 或者我也许做错了…重载的<<如果我把它变成一个内联函数。 但是我怎么让它在我的情况下工作? warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning /tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)' collect2: ld returned […]

如何正确地重载ostream的<<运算符?

我正在用C ++写一个矩阵运算的小型矩阵库。 然而,我的编译器抱怨,之前没有。 这个代码被放置在一个架子上6个月,之间我升级我的电脑从debian蚀刻到lenny(g ++(Debian 4.3.2-1.1)4.3.2),但是我有相同的问题,在Ubuntu系统相同的g ++ 。 这是我的矩阵类的相关部分: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix); } } 而“实施”: using namespace Math; std::ostream& Matrix::operator <<(std::ostream& stream, const Matrix& matrix) { […] } 这是编译器给出的错误: matrix.cpp:459:error:'std :: ostream&Math :: Matrix :: operator <<(std :: ostream&,const Math :: Matrix&)'必须只有一个参数 […]