C++ 两阶段查找运算符<&书信电报;问题
我正在尝试将自己的代码从VS2012移植到g++4.8 我得到了这个顺从错误:C++ 两阶段查找运算符<&书信电报;问题,c++,c++11,g++,C++,C++11,G++,我正在尝试将自己的代码从VS2012移植到g++4.8 我得到了这个顺从错误: AllocationManager.cpp: In member function ‘void AllocationManager::printMemoryLeaks()’: TRLogger.h:247:42: error: ‘streamAggrator’ was not declared in this scope #define TRLOG_INFO streamAggrator(logINFO) <
AllocationManager.cpp: In member function ‘void AllocationManager::printMemoryLeaks()’:
TRLogger.h:247:42: error: ‘streamAggrator’ was not declared in this scope
#define TRLOG_INFO streamAggrator(logINFO) << PACKET_DESCRIPTION << __FUNCTION__ << ":" << __LINE__ << ": "
^
AllocationManager.cpp:39:2: note: in expansion of macro ‘TRLOG_INFO’
TRLOG_INFO << "sdfs\n";
在文件TRLogger.h
中:
enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
class streamAggrator
{
public:
streamAggrator(TLogLevel logLevel);
/* private: */
FILELog fLog;
WarnLog wlog;
std::ostringstream& s1;
std::ostringstream& s2;
};
template<typename T>
streamAggrator& operator<<(streamAggrator& agg, const T& obj)
{
agg.s1 << obj;
agg.s2 << obj;
agg.s2.flush();
return agg;
}
enumtloglevel{logERROR、logWARNING、logINFO、logDEBUG、logDEBUG1、logDEBUG2、logDEBUG3、logDEBUG4};
类流累加器
{
公众:
流量累加器(TLogLevel logLevel);
/*私人:*/
文件日志fLog;
WarnLog wlog;
标准:ostringstream&s1;
标准:ostringstream&s2;
};
模板
streamegrator&operator您当前的问题是,您试图将一个临时streamegrator
对象传递给一个函数,该函数通过非const
引用获取streamegrator
。您不能将临时对象绑定到非const
引用。解决此问题的方法是使输出运算符成为StreamAgrator
的成员:虽然不能将临时变量绑定到非常量
引用,但可以调用非常量
成员函数。请注意,您还将遇到诸如std::flush
之类的操作计算机的问题(问题在于这些是模板本身,您实际上需要一个具体的操作符来调用它们,以便编译器推断出它们的模板参数)
显然,我会正确地解决这个问题,也就是说,我会创建一个std::streambuf
来完成实际工作,而不是试图挖掘一个不创建流的解决方案。您的示例没有做任何有用的事情,也就是说,我真的不知道您想做什么,但代码看起来非常像尝试做类似于teestream
:编写一次,但将输出发送到多个目的地。我在文章中多次发布了相应的流缓冲区(虽然大部分是在Usenet上,但我认为至少在Stackoverflow上也发布过一次)
虽然我不知道如何去掉宏来填充\uuuuuu文件\uuuuuuu
和\uuuu行\uuuuuuu
,但实际的流格式可能应该使用流缓冲区:
struct teebuf: std::streambuf {
private:
std::streambuf* sb1;
std::streambuf* sb2;
public:
teebuf(std::streambuf* sb1, std::streambuf* sb2): sb1(sb1), sb2(sb2) {}
int overflow(int c) {
this->sb1->sputc(c);
this->sb2->sputc(c);
return std::char_traits<char>::not_eof(c);
}
int sync() {
this->sb1->pubsync();
this->sb2->pubsync();
}
};
class logstream
: std::ostream {
std::ofstream out;
teebuf sbuf;
public:
logstream()
: out("file.log")
, sbuf(out.rdbuf(), std::clog.rdbuf()) {
this->init(&this->sbuf);
}
logstream(logstream&& other)
: out(std::move(other.out))
, sbuf(std::move(other.sbuf)) {
this->init(&this->sbuf);
};
struct teebuf:std::streambuf{
私人:
标准::streambuf*sb1;
标准:streambuf*sb2;
公众:
teebuf(std::streambuf*sb1,std::streambuf*sb2):sb1(sb1),sb2(sb2){}
整数溢出(intc){
本->sb1->sputc(c);
本->sb2->sputc(c);
返回std::char_traits::not_eof(c);
}
int sync(){
此->sb1->pubsync();
此->sb2->pubsync();
}
};
类日志流
:std::ostream{
标准:流出流;
蒂布夫;
公众:
日志流()
:out(“file.log”)
,sbuf(out.rdbuf(),std::clog.rdbuf()){
this->init(&this->sbuf);
}
日志流(日志流和其他)
:输出(标准::移动(其他输出))
,sbuf(std::move(other.sbuf)){
this->init(&this->sbuf);
};
我想您可以返回日志流。我不知道您的日志记录级别要做什么,但我猜在准备问题时,它的处理被删除了:可能需要更改实现以适当考虑日志记录级别。您当前的问题是您试图传递一个临时流或
对象到一个函数,该函数通过非
常量引用接受一个流加积器。您不能将临时对象绑定到非常量引用。解决此问题的方法是使输出操作符成为流加积器的成员:而您不能将临时对象绑定到非
常量参考,您可以调用非const
成员函数。请注意,类似std::flush
(问题是这些都是模板本身,您实际上需要一个具体的操作符来调用它们,让编译器推导出它们的模板参数)
显然,我会正确地解决这个问题,也就是说,我会创建一个std::streambuf
来做实际工作,而不是试图挖掘出一个不创建流的解决方案。您的示例没有做任何有用的事,也就是说,我无法真正告诉您正在尝试做什么,但代码看起来非常像是在尝试创建流o类似于teestream
:写一次,但将输出发送到多个目的地。我在帖子中多次发布了相应的流缓冲区(虽然大部分在Usenet上,但我认为至少在Stackoverflow上也发布了一次)
虽然我不知道如何去掉宏来填充\uuuuuu文件\uuuuuuu
和\uuuu行\uuuuuuu
,但实际的流格式可能应该使用流缓冲区:
struct teebuf: std::streambuf {
private:
std::streambuf* sb1;
std::streambuf* sb2;
public:
teebuf(std::streambuf* sb1, std::streambuf* sb2): sb1(sb1), sb2(sb2) {}
int overflow(int c) {
this->sb1->sputc(c);
this->sb2->sputc(c);
return std::char_traits<char>::not_eof(c);
}
int sync() {
this->sb1->pubsync();
this->sb2->pubsync();
}
};
class logstream
: std::ostream {
std::ofstream out;
teebuf sbuf;
public:
logstream()
: out("file.log")
, sbuf(out.rdbuf(), std::clog.rdbuf()) {
this->init(&this->sbuf);
}
logstream(logstream&& other)
: out(std::move(other.out))
, sbuf(std::move(other.sbuf)) {
this->init(&this->sbuf);
};
struct teebuf:std::streambuf{
私人:
标准::streambuf*sb1;
标准:streambuf*sb2;
公众:
teebuf(std::streambuf*sb1,std::streambuf*sb2):sb1(sb1),sb2(sb2){}
整数溢出(intc){
本->sb1->sputc(c);
本->sb2->sputc(c);
返回std::char_traits::not_eof(c);
}
int sync(){
此->sb1->pubsync();
此->sb2->pubsync();
}
};
类日志流
:std::ostream{
标准:流出流;
蒂布夫;
公众:
日志流()
:out(“file.log”)
,sbuf(out.rdbuf(),std::clog.rdbuf()){
this->init(&this->sbuf);
}
日志流(日志流和其他)
:输出(标准::移动(其他输出))
,sbuf(std::move(other.sbuf)){
this->init(&this->sbuf);
};
我想您可以返回日志流。我不知道您的日志记录级别要做什么,但我猜在准备问题时,它的处理被删除了:可能需要更改实现以适当考虑日志记录级别。不要担心#includes-因为它发现了TRLOG\u,请提供一个if可能;特别是函数
void AllocationManager::printMemoryLeaks()
的上下文很有趣。在AllocationManager::printMemoryLeaks()
中有什么?第247行在哪里?可以
struct teebuf: std::streambuf {
private:
std::streambuf* sb1;
std::streambuf* sb2;
public:
teebuf(std::streambuf* sb1, std::streambuf* sb2): sb1(sb1), sb2(sb2) {}
int overflow(int c) {
this->sb1->sputc(c);
this->sb2->sputc(c);
return std::char_traits<char>::not_eof(c);
}
int sync() {
this->sb1->pubsync();
this->sb2->pubsync();
}
};
class logstream
: std::ostream {
std::ofstream out;
teebuf sbuf;
public:
logstream()
: out("file.log")
, sbuf(out.rdbuf(), std::clog.rdbuf()) {
this->init(&this->sbuf);
}
logstream(logstream&& other)
: out(std::move(other.out))
, sbuf(std::move(other.sbuf)) {
this->init(&this->sbuf);
};