gpt4 book ai didi

c++ - 无法设置 ostringstream 对象的 streambuf

转载 作者:太空狗 更新时间:2023-10-29 21:46:15 24 4
gpt4 key购买 nike

我想在我的程序中包含一些 std::ostringstream 对象,用于日志记录和错误报告目的。根据编译时给出的设置,日志和错误流将收集它们各自的数据(要保存或其他),或者它们将被重定向到 std::coutstd: :cerr(用于调试目的)。据我所知 references (和 here )我看过,std::ostringstream 对象继承了 rdbuf() 方法,我应该能够调用 rdbuf() 流对象上的方法,将指向 std::streambuf 的指针作为参数传递以与流关联。

但是,当我尝试编译时,我收到一个错误,因为没有匹配的函数调用,rdbuf() 方法没有任何参数被列为候选。

我从 g++(Ubuntu 12.04 x86_64 上的 4.6.3)得到的错误:

sigma@SigmaSys:~/Scripts/http/test$ g++ test.cc -c -o test.otest.cc: In constructor ‘Logger::Logger()’:test.cc:23:43: error: no matching function for call to ‘std::basic_ostringstream::rdbuf(std::basic_streambuf*)’test.cc:23:43: note: candidate is:/usr/include/c++/4.6/sstream:449:7: note: std::basic_ostringstream::__stringbuf_type* std::basic_ostringstream::rdbuf() const [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator, std::basic_ostringstream::__stringbuf_type = std::basic_stringbuf]/usr/include/c++/4.6/sstream:449:7: note:   candidate expects 0 arguments, 1 provided

Here is a stripped down version of the source and header file:

http_utils.h

#include <string>
#include <sstream>
#include <streambuf>

#define HTTP_DEBUG_MODE 1


class Logger {
private:
static std::ostringstream errOut;
static std::ostringstream logOut;

static std::streambuf* cerrStreamBuffBackup;
static std::streambuf* coutStreamBuffBackup;

static std::streambuf* errOutStreamBuffBackup;
static std::streambuf* logOutStreamBuffBackup;
public:
Logger();
~Logger();
};

http_utils.cc

#include "test.h"
#include <string>
#include <iostream>
#include <sstream>
#include <streambuf>


std::ostringstream Logger::errOut;
std::ostringstream Logger::logOut;
std::streambuf* Logger::cerrStreamBuffBackup = NULL;
std::streambuf* Logger::coutStreamBuffBackup = NULL;
std::streambuf* Logger::errOutStreamBuffBackup = NULL;
std::streambuf* Logger::logOutStreamBuffBackup = NULL;


Logger::Logger() {
if (HTTP_DEBUG_MODE) {//Redirect logging/error to stdout/stderr
Logger::errOutStreamBuffBackup = Logger::errOut.rdbuf();
Logger::logOutStreamBuffBackup = Logger::logOut.rdbuf();

Logger::errOut.rdbuf( std::cerr.rdbuf() );
Logger::logOut.rdbuf( std::cout.rdbuf() );
} else {//Redirect stdout/stderr to custom logging/error functionality
cerrStreamBuffBackup = std::cerr.rdbuf();
coutStreamBuffBackup = std::cout.rdbuf();

std::cerr.rdbuf( errOut.rdbuf() );
std::cout.rdbuf( logOut.rdbuf() );
}
}
Logger::~Logger() {
if (HTTP_DEBUG_MODE) {

} else {//Restore stdout/stderr streambuf
std::cerr.rdbuf( Logger::cerrStreamBuffBackup );
std::cout.rdbuf( Logger::coutStreamBuffBackup );
}
}

如果有人可以帮助我解决这个问题,或者根据我正在尝试做的事情提出替代方法,我将不胜感激。

最佳答案

std::ostringstream objects inherit the rdbuf() method and I should be able to call the rdbuf() method on the stream objects, passing as an argument a pointer to the std::streambuf to associate with the streams.

你应该在Liskov substitution principle方面,但你不能。

std::basic_stringstream::rdbuf隐藏 std::basic_ios::rdbuf() 的两个签名。你可以用一些丑陋的类型转换来称呼他们,但我认为这不会有任何效果。字符串流始终保证写入字符串缓冲区。

关于c++ - 无法设置 ostringstream 对象的 streambuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15721800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com