No copy constructor or copy constructor declared explicit

Can someone explain why I get a compilation error here - error C2558: class 'std :: auto_ptr <_Ty>': no ​​copy constructor is available or the copy constructor is declared "explicit"

#include <memory>
#include <vector>
#include <string>
template<typename T>
struct test
{
    typedef std::auto_ptr<T> dataptr;
    typedef std::auto_ptr< test<T> > testptr;
    test( const T& data ):
    data_( new T(data) )
    {
    };
    void add_other( const T& other )
    {
        others_.push_back( testptr( new test(other) ) );
    }
private:
    dataptr data_;
    std::vector< testptr > others_;
};

int main(int argc, char* argv[])
{
    test<std::string> g("d");

    //this is the line that causes the error.
    g.add_other("d");

    return 0;
}
+5
source share
4 answers

In principle, std::auto_ptrcannot be used in this way.

others_.push_back( testptr( new test(other) ) );

, , const&, std::auto_ptr. , std::auto_ptr ! , , Herb Sutter, , " , " 3/4 .

+6
    others_.push_back( testptr( new test(other) ) );

auto_ptr std::vector

auto_ptr stl.

. : fooobar.com/questions/22980/...

+6

auto_ptr, :

std::vector< testptr > others_;

. , shared_ptr.

+4
+2

All Articles