" 没有成员 "x">
当前位置:   article > 正文

C++ "shared_ptr" 不明确_类 "boost::shared_ptr" 没有成员 "

类 "boost::shared_ptr" 没有成员 "x

运行环境:VS2017,C++,boost_1_68_0-msvc-14.1-64。

问题描述:在运行从官网下载安装的boost库中的shared_ptr智能指针时,程序编译出错,程序代码如下:

  1. #include<iostream>
  2. #include<vld.h>
  3. #include<boost\shared_ptr.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. void main() {
  7. int *p = new int(10);
  8. shared_ptr<int> ps(p);
  9. cout << *ps << endl;
  10. }

错误截图,如下:

原因分析:boost的一些库,比如shared_ptr已经被高版本的STL采纳了,命名空间std和boost中都有shared_ptr,产生冲突。

解决方案1

注释掉代码的第6行代码 “using namespace boost;” 就可以正常编译。

存在的问题:不引入boost命名空间的话,可能无法正常调用boost库中其他成员。

解决方案2

显式指定shared_ptr来自哪个命名空间,使用 boost::shared_ptr 或者 std::shared_ptr。这样做的好处是不影响其他boost库的成员调用,推荐此方案。

  1. #include<iostream>
  2. #include<vld.h>
  3. #include<boost\shared_ptr.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. void main() {
  7. int *p = new int(10);
  8. boost::shared_ptr<int> ps(p);
  9. cout << *ps << endl;
  10. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号