模板错误

shada 2014-12-09 01:22:48
LOG<<"123"<<456; //成功
LOG<<std::endl;//报错


错误 25 error C2563: 在形参表中不匹配
错误 26 error C2568: “<<”: 无法解析函数重载
错误 24 error C2676: 二进制“<<”:“CLog”不定义该运算符或到预定义运算符可接收的类型的转换


各位帮看下。

#pragma once
#include <Windows.h>
#include <string>
#include <iostream>

class CLog
{
private:
CRITICAL_SECTION m_cs;

public:

CLog()
{
InitializeCriticalSection(&m_cs);
}

~CLog()
{
DeleteCriticalSection(&m_cs);
}

template<class T>
CLog& operator<<(T t)
{
EnterCriticalSection(&m_cs);
std::cout << t;
LeaveCriticalSection(&m_cs);
return *this;
}
};

CLog g_log;

#define LOG g_log
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_dragon_fly 2014-12-09
  • 打赏
  • 举报
回复
endl是一个模板函数,参数为basic_ostream, 你的CLog无法转换为basic_ostream, 用'\n代替吧
boygo1982 2014-12-09
  • 打赏
  • 举报
回复
因为 std::endl是一个函数指针。
ri_aje 2014-12-09
  • 打赏
  • 举报
回复
引用 2 楼 ri_aje 的回复:
得加个针对 std::cout 类型的重载。

    CLog& operator << (std::ostream& (*pfn) (std::ostream&))
    {
        std::cout << "here";
        std::cout << pfn;
        return *this;
    }
"here" 那句是多余的,发的时候忘了删了。
ri_aje 2014-12-09
  • 打赏
  • 举报
回复
得加个针对 std::cout 类型的重载。

    CLog& operator << (std::ostream& (*pfn) (std::ostream&))
    {
        std::cout << "here";
        std::cout << pfn;
        return *this;
    }
JiangWenjie2014 2014-12-09
  • 打赏
  • 举报
回复
’\n‘代替std::endl吧,而且std::endl是一个函数。std::cout在看到std::endl时是做特殊处理的。

_CRTIMP2_PURE inline basic_ostream<char, char_traits<char> >&
	__CLRCALL_OR_CDECL endl(basic_ostream<char, char_traits<char> >& _Ostr)
	{	// insert newline and flush byte stream
	_Ostr.put('\n');
	_Ostr.flush();
	return (_Ostr);
	}

	_Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__cdecl *_Pfn)(_Myt&))
		{	// call basic_ostream manipulator
		_DEBUG_POINTER(_Pfn);
		return ((*_Pfn)(*this));
		}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧