#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Problem with an abstract class


    Hi guys,
    I have written the following code, but I can't figure out what's wrong with it:

    Code:
    class Number
    {
    protected:
    	virtual bool operator<(const Number&)=0;
    	virtual bool operator>(const Number&)=0;
    	virtual bool operator==(const Number&)=0;
    };
    
    
    class Int: public Number //Warning: Class 'Int' has virtual method 'operator ==' but non-virtual destructor
    {
    private:
    	int m_int;
    public:
    	Int(int num=0):m_int(num){cout<<"Int Ctr called"<<endl;}
    	~Int(){cout<<"Int Dtr called"<<endl;}
    	bool operator<(const Int&);
    	bool operator>(const Int&);
    	bool operator==(const Int&);
    };
    
    int main()
    {
    	cout<<"Hi!"<<endl;
    	Int anInt;
    }
    I have an error saying:
    "
    cannot declare variable 'anInt' to be of abstract type 'Int'
    because the following virtual functions are pure within 'Int':
    virtual bool Number:: operator<(const Number&)
    virtual bool Number:: operator>(const Number&)
    virtual bool Number:: operator==(const Number&)
    "

    Why can't I create an Int instance?
    Why does it consider it to be abstract, when in fact, I just want Number to be abstract?

    and on top of all that, I have a warning mark set on "class Int: public Number", saying: "Class 'Int' has virtual method 'operator ==' but non-virtual destructor"
    What does the operator== have to do with anything, and why does it raise a warning?

    Thanks in advanced!
    Last edited by so.very.tired; May 6th, 2013 at 04:57 PM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Fixed it for you. Things you got wrong:
    Code:
        bool operator<(const Int&);
        bool operator>(const Int&);
        bool operator==(const Int&);
    should be:
    Code:
        bool operator<(const Number&);
        bool operator>(const Number&);
        bool operator==(const Number&);
    because the parent class abstract functions take Number& as argument, so the three functions you declared in class Int do not implement the abstract functions, but instead create three new functions that take a different argument class (in this case, const Int&). Therefore the original 3 functions remain abstract and unimplemented.

    Also, declare
    Code:
    Int anInt;
    as
    Code:
    Int anInt();
    Here's some fixed up code for you with the fixes highlighted in bold font.
    Code:
    #include <iostream>
    class Number
    {
    protected:
        virtual bool operator<(const Number&)=0;
        virtual bool operator>(const Number&)=0;
        virtual bool operator==(const Number&)=0;
    };
    
    
    class Int: public Number //Warning: Class 'Int' has virtual method 'operator ==' but non-virtual destructor
    {
    private:
    	int m_int;
    public:
        Int(int num=0):m_int(num){std::cout<<"Int Ctr called"<<std::endl;}
        ~Int(){std::cout<<"Int Dtr called"<<std::endl;}
        bool operator<(const Number&);
        bool operator>(const Number&);
        bool operator==(const Number&); 
    };
    
    
    int main()
    {
        std::cout<<"Hi!"<<std::endl;
        Int anInt();
        return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Hi Scorpions4ever.

    this causes another problem.
    take a look at the implementation for this member functions:

    Code:
    bool Int::operator<(const Int& num)
    {
    	return m_int<num.m_int;
    }
    
    
    bool Int::operator>(const Int& num)
    {
    	return m_int>num.m_int;
    }
    
    bool Int::operator==(const Int& num)
    {
    	return m_int==num.m_int;
    }
    Now if I'm to change it according to what you suggested, it'll look like this:


    Code:
    bool Int::operator<(const Number& num)
    {
    	return m_int<num.m_int;
    }
    
    
    bool Int::operator>(const Number& num)
    {
    	return m_int>num.m_int;
    }
    
    bool Int::operator==(const Number& num)
    {
    	return m_int==num.m_int;
    }

    I can't access the num.m_int member now, because the given parameter is a Number object, not an Int object...
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Then you need to rethink your class design. Either make m_int a member of Number or don't declare those abstract functions in the Number class.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    You have to declare the operators as const. C++ won't let you call a non-const function against a const reference. In your virtual base class you should have:

    bool operator < ( const Number& num ) const = 0 ;

    And then the implemenation should have the same signature:

    bool operator < ( const Number& num ) const ;
    I no longer wish to be associated with this site.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    OK, thanks.
    I used cast to solve this issue.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Casting away constness from the parameter is the wrong way to do it. Fix the virtual base class signatures.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo