操作员的重新定义<&书信电报;独身 我在Linux上有一个C++类,允许我在控制台上显示消息,这取决于应用程序运行的位置。如果它在我的电脑中工作,则消息将以控制台模式显示。否则,所有内容都会记录在文本文件中以供以后查看。我创建了一个外部对象来使用它。我想让这个班成为单身学生,但没有成功。我在编译程序时出现了以下错误:
错误:“Log*”和“const char[12]”类型的无效操作数转换为二进制运算符重要提示:不要误会,但您会犯一些错误,这表明您经验不足,很难正确使用单例。对于记录器,它们是可以使用的,只是确保始终使用它们作为最后手段 我没有时间从您的评论中回顾为什么操作员的重新定义<&书信电报;独身 我在Linux上有一个C++类,允许我在控制台上显示消息,这取决于应用程序运行的位置。如果它在我的电脑中工作,则消息将以控制台模式显示。否则,所有内容都会记录在文本文件中以供以后查看。我创建了一个外部对象来使用它。我想让这个班成为单身学生,但没有成功。我在编译程序时出现了以下错误:,c++,linux,pointers,singleton,operator-overloading,C++,Linux,Pointers,Singleton,Operator Overloading,错误:“Log*”和“const char[12]”类型的无效操作数转换为二进制运算符重要提示:不要误会,但您会犯一些错误,这表明您经验不足,很难正确使用单例。对于记录器,它们是可以使用的,只是确保始终使用它们作为最后手段 我没有时间从您的评论中回顾为什么操作符: Log *logOutput; logOutput = Log::createOrGet(); logOutput << "ProcessCommand called" << endl; log
操作符:
Log *logOutput;
logOutput = Log::createOrGet();
logOutput << "ProcessCommand called" << endl;
logOutput << Data << endl;
Log*注销输出;
logOutput=Log::createOrGet();
logOutput在OP的评论中,您发布了以下测试工具:
int main()
{
Log *logOutput;
logOutput = Log::createOrGet();
logOutput << "ProcessCommand called";
logOutput << Data << endl; // I dont know what Data is, so I commented this out
}
按值返回一个Log
,当它超出范围时,将导致一个临时文件被销毁,但是Log::~Log
被声明为private
,因此无法访问
更改您的operator@John他们大多数时候都不是理想的,但总有人在烦你,说他们有多么讨厌他们。正如你所知道的,单身是有用的;只需检查一些Boost库的实现。@Paul:我不是在和你说话。OP可能没有意识到单例常常是一种糟糕的设计技术,这是合情合理的。@John,你不是在跟我说话吧?!你多大年纪@保罗:我只是想指出我没有“打扰”你。我在“窃听”操作。@Jean:请发布复制此问题的测试代码。问题不止这些。@Johndilling你可能是对的,我没有在编译器中测试它。IMHO,代码太复杂了,无论它试图实现什么(这就是为什么我重定向到Paul Manta的评论)。但至少我的回答解释了引用的错误信息。
// The idiomatic way is to call this function 'Instance',
// but this doesn't really matter.
static Log& Instance() {
static Log obj;
return obj;
}
protected:
Log();
Log(const Log&); // CopyConstructor
Log& operator=(const Log&); // AssignmentOperator
~Log();
Log *logOutput;
logOutput = Log::createOrGet();
logOutput << "ProcessCommand called" << endl;
logOutput << Data << endl;
int main()
{
Log *logOutput;
logOutput = Log::createOrGet();
logOutput << "ProcessCommand called";
logOutput << Data << endl; // I dont know what Data is, so I commented this out
}
template<typename T>
Log operator<<( T const& value )
{
(*output) << value;
return *this;
}
template<typename T>
const Log& operator<<( T const& value ) const
{
(*output) << value;
return *this;
}
Log& operator<<( std::ostream&(*f)(std::ostream&) )
{
(*output) << f;
return *this;
}
Log& operator<<(Buffer_T& Buf)
{
if( Buf.size() > 0 )
{
for( unsigned int i = 0; i < Buf.size(); i++ )
{
if( Buf[i] >= 32 && Buf[i] <= 127 )
{
(*output) << Buf[i];
}
else
{
(*output) << "0x" << std::setfill( '0' ) << std::hex << std::setw( 2 ) << unsigned( Buf[i] );
}
}
}
return *this;
}