CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006
    Posts
    23

    Operator Overload problem. Please help

    I have the code as following
    #define PRINT_PER_MINUTE 10;

    class Printer
    {
    private:
    int pages;
    public:
    Printer(int _pages)
    {
    pages = _pages;
    }
    int getPrintTime()
    {
    return pages * PRINT_PER_MINUTE;
    }
    };

    bool Printer:perator<=(const Printer& pri)
    {
    return getPrintTime() <= pri.getPrintTime();
    }

    and I am getting an error message which states that "<= is not a member of Printer".

    What should I do to fix this problem? Please help.

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: Operator Overload problem. Please help

    Code:
    #define PRINT_PER_MINUTE 10
    
    class Printer
    {
    private:
    	int pages;
    public:
    	Printer(int _pages)
    	{
    		pages = _pages;
    	}
    
    	int getPrintTime() const
    	{
    		return pages * PRINT_PER_MINUTE;
    	}
    };
    
    bool operator<=(const Printer& lhs, const Printer& rhs)
    {
    	return lhs.getPrintTime() <= rhs.getPrintTime();
    }
    You either need to declare the operator as part of the class or create it as a function. This code declares it as a function. I also fixed some other issues.

    Jeff

  3. #3
    Join Date
    Oct 2006
    Posts
    23

    Re: Operator Overload problem. Please help

    Thanks for the reply.

    I've tried adding your code, but I still get an error stating

    Printer::getPrintTime' : cannot convert 'this' pointer from 'const Printer' to 'Printer &'

    What should I do to fix that problem?

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: Operator Overload problem. Please help

    My code compiles. I tried it. Try again.

    Jeff
    Last edited by jfaust; October 24th, 2006 at 10:38 PM.

  5. #5
    Join Date
    Oct 2006
    Posts
    23

    Re: Operator Overload problem. Please help

    Oh it worked for me too. Just forgot to put const on my return function. Thanks

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Operator Overload problem. Please help

    1. Get rid of the #define
    2. use explicit constructor and initialiser list:
    3. If the operator is overloaded inside a header declare it inline.
    4 However you see a wastage because both of the pages variables are being multiplied by a constant so you know a small optimisation to operator<=. However that means accessing the private variable. You therefore declare it first as a friend.

    Code:
    class Printer
    {
    private:
            enum { PRINT_PER_MINUTE = 10 };
    
    	int pages;
    public:
    	explicit Printer(int _pages) : pages ( _pages )
    	{
    	}
    
    	int getPrintTime() const
    	{
    		return pages * PRINT_PER_MINUTE;
    	}
                  friend bool operator<= ( const Printer & lhs, const Printer & rhs );
    };
    
    inline bool operator<=(const Printer& lhs, const Printer& rhs)
    {
    	return lhs.pages <= rhs.pages;
    }
    Last edited by NMTop40; October 25th, 2006 at 03:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured