Re: auto_ptr compile error

Tech-Archive recommends: Fix windows errors by optimizing your registry



George wrote:
Hello everyone,


Two questions,

1.

The following compiler error from the following code segment, is because delete statement from destructor of auto_ptr requires the declaration of destructor of wrapped object?

[Code]
1>main.cpp
1>d:\visual studio 2008\projects\test_autoptr2\test_autoptr2\main.cpp(10) : error C2512: 'Foo' : no appropriate default constructor available

#include <memory>
#include <iostream>

using namespace std;

class Foo;

void func()
{
auto_ptr<Foo> pf (new Foo());

cout << "I am here. " << endl;

return;
}

int main()
{
func();

return 0;
}

class Foo {
};
[/Code]

2.

If yes, how to define a destructor outside of the body of class declaration? I defined in this way, but compile can not pass.

[Code]
Foo::~Foo()
{

}
[/Code]

George:

The error message says nothing about destructors.

If you move the definition of class Foo to above func() then it will compile. The default constructor and destructor of Foo will be used.

--
David Wilkinson
Visual C++ MVP
.



Relevant Pages

  • Re: auto_ptr compile error
    ... "David Wilkinson" wrote: ... destructor of wrapped object? ... If you move the definition of class Foo to above functhen it will compile. ...
    (microsoft.public.vc.language)
  • Re: auto_ptr compile error
    ... but compile can not pass. ... you need to declare the destructor inside the ... and it could be dangerous sitting under them as they fly ...
    (microsoft.public.vc.language)
  • auto_ptr compile error
    ... The following compiler error from the following code segment, ... destructor of wrapped object? ... how to define a destructor outside of the body of class declaration? ...
    (microsoft.public.vc.language)
  • error while doing make in php-5.2.1
    ... declaration has no <typename> ...
    (comp.lang.php)
  • Re: auto_ptr compile error
    ... Do you mean move definition of ~Ownerbefore full declaration of Thing or after full declaration of Thing? ... I do not think the error has something to do with destructor of ~Owner() as you mentioned, ... There seems general agreement that "deletion of incomplete type" should be an error, rather than a warning, because it can lead to memory leaks. ...
    (microsoft.public.vc.language)