|
-
October 24th, 2006, 09:22 PM
#1
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.
-
October 24th, 2006, 09:28 PM
#2
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
-
October 24th, 2006, 09:50 PM
#3
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?
-
October 24th, 2006, 10:09 PM
#4
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.
-
October 24th, 2006, 10:14 PM
#5
Re: Operator Overload problem. Please help
Oh it worked for me too. Just forgot to put const on my return function. Thanks
-
October 25th, 2006, 01:27 AM
#6
Re: Operator Overload problem. Please help
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 25th, 2006, 03:43 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|