Thread: auto_ptr compile error

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    auto_ptr compile error

    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 {
    };
    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()
    {
    
    }

    thanks in advance,
    George

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You discovered that smart pointers place certain requirements on the pointed-to object. An auto_ptr cannot use an incomplete type. It is explicitly forbidden in the standard. Even though you define the type further down, it has to be complete at the point of template instantiation.

    And it's crazy to think you can just define a Foo::~Foo() without also changing the class declaration itself. The solution isn't to write a destructor, it's to use a complete type instead of an incomplete one.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks brewbuck,


    1.

    Quote Originally Posted by brewbuck View Post
    You discovered that smart pointers place certain requirements on the pointed-to object. An auto_ptr cannot use an incomplete type. It is explicitly forbidden in the standard. Even though you define the type further down, it has to be complete at the point of template instantiation.
    You mean before using auto_ptr, the whole declaration of class should be before it?

    2.

    Quote Originally Posted by brewbuck View Post
    And it's crazy to think you can just define a Foo::~Foo() without also changing the class declaration itself. The solution isn't to write a destructor, it's to use a complete type instead of an incomplete one.
    How to fix? I have tried in this way, but still can not compile.

    Code:
    #include <memory>
    #include <iostream>
    
    using namespace std;
    
    class Foo;
    
    Foo::~Foo()
    {
    }
    
    void func()
    {
    	auto_ptr<Foo> pf (new Foo());
    
    	cout << "I am here. " << endl;
    
    	return;
    }
    
    int main()
    {
    	func();
    
    	return 0;
    }
    
    class Foo {
    public:
    ~Foo()
    {
    }
    
    };

    regards,
    George

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It still won't compile because you still have not defined the type before the auto_ptr is declared.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    This code needs to come before the auto_ptr instantiation:
    Code:
     class Foo {
    };
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks King,


    I think put only declaration of class, including constructor and destructor declaration is enough, right?


    Quote Originally Posted by King Mir View Post
    This code needs to come before the auto_ptr instantiation:
    Code:
     class Foo {
    };

    Thanks bithub,


    I think declaration of class (including constructor and destructor) is enough?

    Quote Originally Posted by bithub View Post
    It still won't compile because you still have not defined the type before the auto_ptr is declared.

    regards,
    George

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You realize that putting an incomplete definition or a forward declaration of a class, and then using that class is one the worst possible things you can do, right?
    The compiler cannot call the constructor nor destructor (you get a warning) and you cannot call any member functions either (you get an error).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I do not agree with you, forward declaration is ok and we can make an incomplete class declaration and complement the implementation later.

    Here is my code to show this idea.

    Code:
    #include <memory>
    #include <iostream>
    
    using namespace std;
    
    class Foo {
    public:
    	Foo();
    	~Foo();
    };
    
    void func()
    {
    
    	auto_ptr<Foo> pf (new Foo());
    
    	cout << "I am here. " << endl;
    
    	return;
    }
    
    int main()
    {
    	func();
    
    	return 0;
    }
    
    Foo::Foo()
    {
    }
    
    Foo::~Foo()
    {
    }
    Quote Originally Posted by Elysia View Post
    You realize that putting an incomplete definition or a forward declaration of a class, and then using that class is one the worst possible things you can do, right?
    The compiler cannot call the constructor nor destructor (you get a warning) and you cannot call any member functions either (you get an error).

    regards,
    George

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Thanks Elysia,

    I do not agree with you, forward declaration is ok and we can make an incomplete class declaration and complement the implementation later.

    Here is my code to show this idea.

    regards,
    George
    This is not a forward declaration.
    This is the standard way of doing things.
    What's bad is putting

    class Foo;

    And then actually using the class as you were doing before.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I have got your points about what means a forward declaration. :-)

    Quote Originally Posted by Elysia View Post
    This is not a forward declaration.
    This is the standard way of doing things.
    What's bad is putting

    class Foo;

    And then actually using the class as you were doing before.

    regards,
    George

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by George2 View Post
    I do not agree with you, forward declaration is ok and we can make an incomplete class declaration and complement the implementation later.
    Well, Elysia is correct and you are misinterpreting your example.

    The code in your example is valid because it does not make use of any forward declarations, and presents a complete class definition before func() attempts to instantiate your class.

    The code;
    Code:
    class Foo {
    public:
    	Foo();
    	~Foo();
    };
    is neither a forward declaration nor an incomplete class declaration. It is a class definition.

    The subsequent implementations of the constructor and destructor are also definitions of those (special) functions. The compiler does not need to see those definitions (in fact, they can be put into separate source files) because the previous class definition declares them, so they may be called.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks grumpy,


    Question answered.

    Quote Originally Posted by grumpy View Post
    Well, Elysia is correct and you are misinterpreting your example.

    The code in your example is valid because it does not make use of any forward declarations, and presents a complete class definition before func() attempts to instantiate your class.

    The code;
    Code:
    class Foo {
    public:
    	Foo();
    	~Foo();
    };
    is neither a forward declaration nor an incomplete class declaration. It is a class definition.

    The subsequent implementations of the constructor and destructor are also definitions of those (special) functions. The compiler does not need to see those definitions (in fact, they can be put into separate source files) because the previous class definition declares them, so they may be called.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM