在VC6中使用boost1.33.1的lexical_cast编译不能通过的问题

dwlitt 2007-04-14 01:40:58
#include <boost/lexical_cast.hpp>
#include <iostream>

void boost_lexical()
{
using boost::lexical_cast;
int a=10;
double b=20;

a = lexical_cast<int>("123");
b = lexical_cast<double>("123.12");
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
}
int main()
{
boost_lexical();
return 0;
}
//////////////////////////////////
Compiling...
main.cpp
F:\000\boost\myboost\main.cpp(15) : error C2062: type 'int' unexpected
F:\000\boost\myboost\main.cpp(16) : error C2062: type 'double' unexpected
Error executing cl.exe.

myboost.exe - 2 error(s), 0 warning(s)
////////////////////////////////////////
为什么不能编译通过呢?哪位大大告诉一生?
我的环境:VC6+SP6+WINXPSP2
另:boost的包含路径已经配置好了的
...全文
280 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dwlitt 2007-04-14
  • 打赏
  • 举报
回复
asdf
dwlitt 2007-04-14
  • 打赏
  • 举报
回复
果然是VC6中using的问题,在VC8(.NET2005)中编译就没有问题!
解决的方法:(两种都可以的,随便选用)
(1)引进命名空间:
//using boost::lexical_cast; //这个在VC6中有问题的,不能引进名字空间boost
using namespace boost;

(2)调用时加上命名空间:
//a = lexical_cast<int>("123"); //不带名字空间
//b = lexical_cast<double>("123.12"); //不带名字空间,有问题

a = boost::lexical_cast<int>("123");
b = boost::lexical_cast<double>("123.12"); //OK, 没问题

////////////////////////////////////////////////
谢谢楼上的,回头给分!
roger_77 2007-04-14
  • 打赏
  • 举报
回复
改为:

//using boost::lexical_cast;
int a=10;
double b=20;

a = boost::lexical_cast<int>("123");
b = boost::lexical_cast<double>("123.12");



命名空间与模板特化的问题.
orc1984 2007-04-14
  • 打赏
  • 举报
回复
也有可能你库没有加对路径
Aresky 2007-04-14
  • 打赏
  • 举报
回复
写成 a = boost::lexical_cast<int>("123"); 试试
sinall 2007-04-14
  • 打赏
  • 举报
回复
#include <boost/lexical_cast.hpp>
#include <iostream>
void boost_lexical()
{
using namespace boost;
int a=10;
double b=20;

a = lexical_cast<int>("123");
b = lexical_cast<double>("123.12");
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
}
int main()
{
boost_lexical();
return 0;
}

65,213

社区成员

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

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