" 没有成员 "x">
赞
踩
运行环境:VS2017,C++,boost_1_68_0-msvc-14.1-64。
问题描述:在运行从官网下载安装的boost库中的shared_ptr智能指针时,程序编译出错,程序代码如下:
- #include<iostream>
- #include<vld.h>
- #include<boost\shared_ptr.hpp>
-
- using namespace std;
- using namespace boost;
-
- void main() {
- int *p = new int(10);
- shared_ptr<int> ps(p);
- cout << *ps << endl;
- }
错误截图,如下:

原因分析:boost的一些库,比如shared_ptr已经被高版本的STL采纳了,命名空间std和boost中都有shared_ptr,产生冲突。
注释掉代码的第6行代码 “using namespace boost;” 就可以正常编译。
存在的问题:不引入boost命名空间的话,可能无法正常调用boost库中其他成员。
显式指定shared_ptr来自哪个命名空间,使用 boost::shared_ptr 或者 std::shared_ptr。这样做的好处是不影响其他boost库的成员调用,推荐此方案。
- #include<iostream>
- #include<vld.h>
- #include<boost\shared_ptr.hpp>
-
- using namespace std;
- using namespace boost;
-
- void main() {
- int *p = new int(10);
- boost::shared_ptr<int> ps(p);
- cout << *ps << endl;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。