auto_ptr<ifstream> warning?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: auto_ptr<ifstream> warning?

  1. #1
    Join Date
    Nov 2003
    Posts
    1,405

    auto_ptr<ifstream> warning?

    When I try to declare this in a header file I get a warning.
    Code:
    auto_ptr<ifstream> ifs;
    The program works but how can I get rid of this warning?

    C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\memory(484) : warning C4150: deletion of pointer to incomplete type 'std::basic_ifstream<_Elem,_Traits>'; no destructor called
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\memory(484) : see reference to class template instantiation 'std::basic_ifstream<_Elem,_Traits>' being compiled
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\memory(483) : while compiling class-template member function 'std::auto_ptr<_Ty>::~auto_ptr(void)'
    with
    [
    _Ty=std::ifstream
    ]
    c:\MINT\mint1\XLoadAtoms.h(13) : see reference to class template instantiation 'std::auto_ptr<_Ty>' being compiled
    with
    [
    _Ty=std::ifstream
    ]

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405
    Originally posted by Andreas Masur
    [Moved thread]
    I'm moved

    I found out. I just forgot to include this header file,
    Code:
    #include < fstream>
    Gosh do I hate to be a newbie.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405
    The original code should be,
    Code:
    auto_ptr< ifstream> ifs;
    I can't handle this quite yet.

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    What are you trying to do here ?

    The purpose of auto_ptr is to delete memory which you have newed e.g.

    Code:
    {
        auto_ptr<MyClass> pMyClass(new MyClass);
    
        // to use
        pMyClass->SomeFunction();
    }
    
    // gone out of scope, MyClass destroyed.
    When the function containing the auto_ptr returns, then the newed memory will be released.

    I don't understand why you're trying to use it with an ifstream.

    Darwen.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Sorry, the site got rid of my angle brackets

    Code:
    auto_ptr[<]pMyClass(new MyClass)[>];


    Darwen.

  7. #7
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Nope, try again.

    Code:
    auto_ptr&gt;CMyClass&lt;(new CMyClass);
    Darwen.

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    This is it !

    Code:
    auto_ptr&lt;CMyClass&gt;(new CMyClass);
    At last.

    Darwen.

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Oh god, try again

    Code:
    auto_ptr&lt;CMyClass&gt; pMyClass(new CMyClass);

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405
    Originally posted by darwen
    What are you trying to do here ?

    The purpose of auto_ptr is to delete memory which you have newed e.g.
    I'm declaring an auto_ptr< ifstream> variable in the header file of a class. At some point a new fstream is assigned to it. The purpose is to make sure the fstream gets destructed for sure. I've read in Effective C++ that the use of auto_ptr's is a way to ensure that. If I for example assign a new ifstream to this auto_ptr variable the old ifstream will get automatically deleted. To me it seams like a fool-proof way to avoid leaks?

  11. #11
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Fair dues. You are right of course, you should be using auto_ptr to release any memory which is created.

    Darwen.

  12. #12
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515

    Talking

    Originally posted by darwen
    Oh god, try again
    Dude! Use the EDIT button...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center