Where is shared_ptr?

前端 未结 4 966
甜味超标
甜味超标 2020-11-29 19:56

I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for s

相关标签:
4条回答
  • 2020-11-29 20:29

    Boost Getting Started

    If you want to use it from Boost TR1 instead

    shared_ptr Example

    0 讨论(0)
  • 2020-11-29 20:41

    There are at least three places where you may find shared_ptr:

    1. If your C++ implementation supports C++11 (or at least the C++11 shared_ptr), then std::shared_ptr will be defined in <memory>.

    2. If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++'s libstdc++). Boost also provides a TR1 implementation that you can use.

    3. Otherwise, you can obtain the Boost libraries and use boost::shared_ptr, which can be found in <boost/shared_ptr.hpp>.

    0 讨论(0)
  • 2020-11-29 20:48

    for VS2008 with feature pack update, shared_ptr can be found under namespace std::tr1.

    std::tr1::shared_ptr<int> MyIntSmartPtr = new int;
    

    of

    if you had boost installation path (for example @ C:\Program Files\Boost\boost_1_40_0) added to your IDE settings:

    #include <boost/shared_ptr.hpp>
    
    0 讨论(0)
  • 2020-11-29 20:48

    If your'e looking bor boost's shared_ptr, you could have easily found the answer by googling shared_ptr, following the links to the docs, and pulling up a complete working example such as this.

    In any case, here is a minimalistic complete working example for you which I just hacked up:

    #include <boost/shared_ptr.hpp>
    
    struct MyGizmo
    {
        int n_;
    };
    
    int main()
    {
        boost::shared_ptr<MyGizmo> p(new MyGizmo);
        return 0;
    }
    

    In order for the #include to find the header, the libraries obviously need to be in the search path. In MSVC, you set this in Project Settings>Configuration Properties>C/C++>Additional Include Directories. In my case, this is set to C:\Program Files (x86)\boost\boost_1_42

    0 讨论(0)
提交回复
热议问题