Thread: destructor

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0

    destructor


    ive got this error:
    error C2512: 'RndNum' : no appropriate default constructor available

    and im using this code:
    // Implementing the reel.h file into reel.cpp //

    #include "Reel.h"
    #include "RndNum.h"

    // Construction/Destruction //

    int REEL::spin()
    {
    RndNum ran;
    currentSymbol = ran.generateNumber();
    return currentSymbol;
    }

    int REEL::getCurrentSymbol()
    {
    return spin();
    }

    im using the RndNum class which hold this data:

    // RndNum.h: interface for the RndNum class.
    //
    //////////////////////////////////////////////////////////////////////

    #include <ctime>
    #include <cstdlib>

    class RndNum
    {
    public:
    RndNum(int low, int high);
    int generateNumber();
    virtual ~RndNum();

    private:
    int lowNumber;
    int highNumber;

    };

    any1 tell me what ive done wrong???
    or how to amend it
    cheers
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    Create a dummy constructor
    RndNum()
    {
    }
    I think that should help
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    After you declared your default constructor here:

    virtual ~RndNum();

    you probably never defined it. That statement is a function declaration, and it requires a function definition somewhere. Your program calls the destructor whenever a function creates an object and then returns--all local objects are destoyed when a function ends--and that's when your destructor was called, and the compiler couldn't find the definition.

    If you don't want your destructor to do anything extra just declare it and define it to do nothing at the same time:

    virtual ~RndNum()
    {

    }

    or as its more commonly written:

    virtual ~RndNum() {}

    (don't forget: no semi-colon is used after a function definition)

    or don't declare it at all, and the compiler will supply a default constructor.
    Last edited by 7stud; August 4th, 2003 at 08:43 PM.

IMN logo majestic logo threadwatch logo seochat tools logo