Thread: Destructors and Constructors on classes

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Destructors and Constructors on classes

    Hi.
    I have a C++ book right in front of me here, and it suggest making a destructor like this:

    Code:
    class SomeClass {
    public:
    ~SomeClass ();  // Destructor definition
    };
    
    
    SomeClass::~SomeClass () {
    
    }

    But when i do this, exactly like the book says, i get this one:
    Code:
      multiple definition of `SomeClass::~SomeClass()' 
      first defined here 
      ld returned 1 exit status 
      Makefile.win 
     [Build Error]  [YAPI.exe] Error 1
    Looks like it doesent allow me to first define it, and then implement it...
    Is it that this isnt, allowed, and that my C++ book is wrong?

    Or is it a syntax error somewhere?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you copied this verbatim from the book, then you've got a bad book.

    The comment that says "destructor definition" is wrong. This line is the destructor declaration. The definition is further down, the SomeClass::~SomeClass thing.

    The definition must go into a source file, not a header file. For example, you might have created a pair of files for SomeClass, like SomeClass.h and SomeClass.cpp. The destructor definition goes into SomeClass.cpp, while the class definition (including the destructor declaration) goes into SomeClass.h.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Okay, i have now remade my example:

    SomeClass.h
    Code:
    class SomeClass {
    public:
    ~SomeClass (); 
    };

    SomeClass.cpp
    Code:
    SomeClass::~SomeClass () {
    
    }
    I still get the same error... ideas?

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    73
    You do not have to put the code in two seperate files for it to work..

    I often write both my class specification and definition in the .h to make writing easier and then move the definition part to a separate .cpp later.
    As for your error I do not know... thats how I would write it..

    perhaps you should add a constructor implicitly...
    Someclass();
    ~Someclass();

    Though I doubt that is the problem..

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You still get the same error? Hmm...

    Are you #include'ing the .cpp file anywhere?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    No, im not including the .cpp file anywhere..
    (but i include the .h file in hte .cpp file, but thats self-explanatory...)


    Could it be my compiler?
    (Dev C++ 4.9.9.2?)

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, but perhaps your compilation process. Have you recompiled every .cpp file that included the .h file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The definition must go into a source file
    It should go into a source file, but it doesn't have to. It can be defined inline in the header (has some uses though not many).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    Someclass::Someclass()
    {
        ~Someclass();
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    still doesent work..
    While THIS works:

    Code:
    class SomeClass {
    public:
    ~SomeClass () {
    //and here I go, as long as i make EVERYTHING within the declaration
    }
    
    
    };
    Atleast, then the compiler shuts up and let me compile...
    But im a man of principles, and this is a ugly and uncorrect way to do it! d:

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What lines does the error message point at? It says "first defined here", where is here?

    Make sure you don't have
    Code:
    ~SomeClass() { }
    instead of
    Code:
    ~SomeClass();
    as the declaration.

  12. #12
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Also, though I dont think this is what you have, in your implementation make sure you dont have:

    Code:
    ~SomeClass(); { }
    The semicolon would make it appear as another definition.
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  13. #13
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Umm....I use Dev-C++ as well, and the code you posted in your very first post compiles and runs fine for me. Your book was correct. Your error is somewhere else. If you get a multiple definition error, then that's just what it is. You've probably #included the .h file more than once, therefore, it is defined/declared more than once. Here's what I have.

    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #include "class.h"
    
    int main(int argc, char *argv[])
     {
      system("PAUSE");
      return EXIT_SUCCESS;
     }
    class.h
    Code:
    class SomeClass
     {
      public:
        ~SomeClass();  // Destructor declaration
     };
    
    SomeClass::~SomeClass()
     {
    
     }
    Last edited by Jaken Veina; 06-14-2005 at 01:10 PM.

  14. #14
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    ... I manually deleted all makefiles, objectfiles and various project-builds, restarted my computer, and clicked "rebuild all", and it finaly worked !

  15. #15
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Heh. Dev-C++ is wierd, At least you got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors and Destructors
    By lasher in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2006, 11:53 PM
  2. Self-Referrential Classes
    By Dogplatter in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2006, 08:48 PM
  3. Constructors and Destructors
    By GravtyKlz in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2003, 10:44 AM
  4. Constructors And Destructors... What's The Point?
    By DeanDemon in forum C++ Programming
    Replies: 7
    Last Post: 12-15-2002, 12:47 PM
  5. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM