Click to See Complete Forum and Search --> : auto_ptr<ifstream> warning?


_uj
December 17th, 2003, 12:09 PM
When I try to declare this in a header file I get a warning.

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
]

Andreas Masur
December 17th, 2003, 12:18 PM
[Moved thread]

_uj
December 17th, 2003, 12:24 PM
Originally posted by Andreas Masur
[Moved thread]

I'm moved :)

I found out. I just forgot to include this header file,

#include < fstream>

Gosh do I hate to be a newbie.

_uj
December 17th, 2003, 12:28 PM
The original code should be,

auto_ptr< ifstream> ifs;

I can't handle this quite yet.

darwen
December 18th, 2003, 10:37 AM
What are you trying to do here ?

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


{
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.

darwen
December 18th, 2003, 10:41 AM
Sorry, the site got rid of my angle brackets


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




Darwen.

darwen
December 18th, 2003, 10:42 AM
Nope, try again.


auto_ptr&gt;CMyClass&lt;(new CMyClass);


Darwen.

darwen
December 18th, 2003, 10:44 AM
This is it !


auto_ptr&lt;CMyClass&gt;(new CMyClass);


At last.

Darwen.

darwen
December 18th, 2003, 10:45 AM
Oh god, try again


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

_uj
December 18th, 2003, 11:30 AM
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?

darwen
December 18th, 2003, 04:29 PM
Fair dues. You are right of course, you should be using auto_ptr to release any memory which is created.

Darwen.

vicodin451
December 18th, 2003, 10:42 PM
Originally posted by darwen
Oh god, try again


Dude! Use the EDIT button...:rolleyes: