Simpl shared_ptr declaration

This is a discussion on Simpl shared_ptr declaration within the C++ Programming forums, part of the General Programming Boards category; Hi, I did a search but all I found were topics too advanced for me to understand. I would like ...

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

    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
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    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
    87

    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
    21,720
    My guess is that you did not add the Boost source directory to your compiler's list of directories to search for header files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    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
    87
    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, 02:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21