Thread: Simpl shared_ptr declaration

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    199

    Cool Simpl shared_ptr declaration

    Hi,

    I did a search but all I found were topics too advanced for me to understand. I would like to simply declare a shared_ptr and know what includes I need to do it. I've seen some people use boost:: and some use std::tr1 which confuses me as I've never heard of boost or tr1 and I hear raw pointers are on the way out from modern code so I need to learn to use smart ones.

    Can anyone help? I tried:
    Code:
    #include <memory>
    
    int main ()
    {
        std::shared_ptr<int> myPointer;
    
        return 1;
    }
    But my compiler rejected it. ANy ideas? Thanks so much.

    Chris

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the moment, shared_ptr is not part of the C++ standard, although it is expected to be in the next version of the C++ standard. However, it is part of the Technical Report 1 (TR1) extensions to the C++ standard library. As such, you should write:
    Code:
    #include <memory>
    
    int main()
    {
        std::tr1::shared_ptr<int> myPointer;
        // ...
    }
    This assumes that you are using a standard library implementation that has support for TR1. If not, this will probably fail to compile.

    Now, shared_ptr was developed for the Boost C++ Libraries, which serves as a kind of "breeding ground" for libraries that may eventually be included in the C++ standard library. This explains why there is boost::shared_ptr. If it turns out that you are unable to use TR1, then one place to find a TR1 implementation is Boost.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    199
    That's great thanks. I still get the compile error but at least now thanks to you I know why. I think I need to do a bit of research on how to get my IDE to use the boost library. My present stuff doesn't have the shared_ptr under TR1 if that makes sense

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    199

    Question

    Hi I've installed boost but my IDE won't recognise the files it calls:

    Code:
    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        using namespace boost::lambda;
        typedef std::istream_iterator<int> in;
    
        std::for_each(
            in(std::cin), in(), std::cout << (_1 * 3) << " " );
    }
    I don't really understand the code but I can understand files and include stuff. In my compiler settings I have the boost subdir in my search directories but it still can't find the stuff. Any ideas? Thanks to anyone who replies.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My guess is that you did not add the Boost source directory to your compiler's list of directories to search for header files.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    199
    That's exactly what i thought. I checked it out though seems ok:

    C:\Program Files\boost\boost_1_44_0\boost

    That's where they all are. Ah no matter if I can't do it, teachs me for being a n00b! Thanks anyway mate.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    199
    P.S. it's fixed. I had overrun the include path by one *boost* too many! The path should've stopped at:

    C:\Program Files\boost\boost_1_44_0

    instead I had it at

    C:\Program Files\boost\boost_1_44_0\boost

    Whoops!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM