类模版实例化问题(STL)

FongLuo 2007-04-24 03:44:57
相关定义:

template< class char_type, class traits_type >
class basic_message_handler_log
: public std::basic_ostream< char_type, traits_type>
{...}

template< class char_type, class traits_type>
class basic_thread_safe_log
// *** protected, not public !!!
: protected basic_message_handler_log< char_type, traits_type>
{...}

typedef basic_thread_safe_log< char> thread_safe_log;

// 根据数字取得字符串
template< int idxLog> std::string get_out_name(
bool bIsSharedLog,
int_to_type< idxLog> * = NULL /* workaround for VC6 bug */)
{
std::ostringstream out;
out << "out" << (bIsSharedLog ? "sharedthread" : "ownthread")
<< idxLog << ".txt";
return out.str();
}


在VS 2005下:
如下代码可以编译通过:
1、
template< class char_type, class traits_type>
basic_thread_safe_log< char_type, traits_type>
get_log_sharedthread()
{
const basic_string< char_type, traits_type> & strFileName;
static std::ofstream out( strFileName );
static internal_thread_safe_log_sharethread log( out, get_ts_writer(), 1);
return basic_thread_safe_log< char_type, traits_type>( log);
}

2、
template< int idxLog> thread_safe_log templ_get_log_sharedthread(int_to_type< idxLog> * = NULL /* workaround for VC6 bug */)
{
static std::ofstream out( get_out_name< idxLog>( true).c_str() );
static internal_thread_safe_log_sharethread log( out, get_ts_writer(), 10 * ( idxLog + 1) * ( idxLog + 1));
return thread_safe_log( log);
}

如下代码编译失败:
thread_safe_log get_log_sharedthread(const string & strFileName)
{
static std::ofstream out( strFileName );
static internal_thread_safe_log_sharethread log( out, get_ts_writer(), 1);
return thread_safe_log( log);
}

请问为何?
如何修改使编译失败的代码能够通过编译?
...全文
301 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
FongLuo 2007-04-25
  • 打赏
  • 举报
回复
其实这个就是正解。

没有#include <string>
using std::string吧。

或把
thread_safe_log get_log_sharedthread(const string & strFileName)
改成
thread_safe_log get_log_sharedthread(const std::string & strFileName)
就可以了
taodm 2007-04-24
  • 打赏
  • 举报
回复
呵呵,现在是不太写简单模板了,已经过了那个阶段了。
FongLuo 2007-04-24
  • 打赏
  • 举报
回复
to taodm((不能收CSDN社区短信息,请莫浪费精力))

这个问题不难,要不你试试?
taodm 2007-04-24
  • 打赏
  • 举报
回复
编程大部分问题都是心静不下来造成的。
FongLuo 2007-04-24
  • 打赏
  • 举报
回复
不好意思,问题已经解决;可惜不能给自己加分。

办法有两个,一个25分。:D
taodm 2007-04-24
  • 打赏
  • 举报
回复
那你就把无关代码都砍光,看最后结果吧。
FongLuo 2007-04-24
  • 打赏
  • 举报
回复
那些头文件什么的都没有问题的。

我诊断是在于模版参数的推导,但不知道错在哪。
FongLuo 2007-04-24
  • 打赏
  • 举报
回复
我把第一个能通过编译的函数定义修改了一下:
template< class char_type, class traits_type>
basic_thread_safe_log< char_type, traits_type>
get_log_sharedthread(const basic_string< char_type, traits_type> & strFileName)
{

static std::ofstream out( strFileName );
static basic_internal_thread_safe_log_sharethread< char_type, traits_type> log( out, get_ts_writer(), 1);
return basic_thread_safe_log< char_type, traits_type>( log);
}

除了第3个错误,其它依旧。
taodm 2007-04-24
  • 打赏
  • 举报
回复
没有#include <string>
using std::string吧。
FongLuo 2007-04-24
  • 打赏
  • 举报
回复
不好意思,忘写了。提示如下:
.\XX.cpp(1062) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
.\XX.cpp(1062) : error C2143: 语法错误 : 缺少“,”(在“&”的前面)
.\XX.cpp(1064) : error C2065: “strFileName”: 未声明的标识符

其中的1062行指的就是 thread_safe_log get_log_sharedthread(const string & strFileName) 这一行
lidongri 2007-04-24
  • 打赏
  • 举报
回复
static internal_thread_safe_log_sharethread log( out, get_ts_writer(), 1);
return thread_safe_log( log);
加上模版把
taodm 2007-04-24
  • 打赏
  • 举报
回复
啥错误?

64,654

社区成员

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

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