C++
这是一种尝试,使类模拟使用<<操作符ofstream的输出行为,只要能够使用std::endl和编写strings就行了。该类只有一个数据成员,即ofstream指针。该类有两个重载的<<操作符,一个接受std::string,另一个接受指向函数的指针,其参数是ostream引用并返回ostream引用。这是std::endl的签名,根据的说法。从技术上讲,下面的程序与给定的输入一起工作。它可以打印到文件,两行文本由两个std::endl隔开。然而,我希望我的非-string参数重载<<操作符只接受std::endl,而不仅仅是与其签名相匹配的东西。
我已经编写了一个程序,其中有三个类,每个类彼此继承,但是当我尝试创建一个派生类函数时,cout会给出这样的错误:
3 IntelliSense: no suitable user-defined conversion from "std::basic_ostream<char, std::char_traits<char>>" to "std::string" exists e:\Visual Studio Projects\test\test\Source.cpp 19 10 test
我应该改变什么,什么是解决办法。如果
问题:
#include <iostream>
#include <sstream>
class MyClass : private std::ostream
{
public :
MyClass() : std::ostream(&mBuffer) { }
using std::operator<<;
private:
std::stringbuf mBuffer;
};
// In main function, for example
MyClass c;
c << 'A' <
没有成员函数close()。什么类型的溪流不应该让我关闭?
作为一个例子,也许我想关闭std::cout,以防止它的任何进一步的写作。
std::cout.close(); // ‘std::ostream’ has no member named ‘close’
如果我使用的是C库,我可以用以下方法关闭stdout:
fclose(stdout); // no problem
那么,将close()成员从std::ostream中剔除的背后是什么想法呢?
相关信息:
我有两个类,有两个全局friend oparator<<。
class A {
friend std::ostream& operator<<(std::ostream& o, const A &a);
};
class B: public A {
friend std::ostream& operator<<(std::ostream& o, const B &b);
};
如果我这样使用它,一切都很好,运算符的B版本被调用:
B b;
std::cout << b;
但是,如果
我们有一个基于std::ostream和自定义流缓冲区的日志实现。我们通过Schwarz计数器实现应用程序的log类实例。
为了避免将较低级别的类耦合到日志实现,我们可以传递一个对std::ostream的引用。通过这种方式,我们的低级类可以记录到std::cout、std::cerr或通过Schwarz计数器创建的实例。
我对此有一个问题。日志实现通过流运算符的重载来设置其严重性:
// Overload the << operator to set the log message severity
inline CLogStream& operator <<