|
Thank you!
But I don't know how to pass it to sort().
I write my code so:
class MyClass {
private:
char m_myVar1[255];
char m_myVar2[255];
public:
MyClass();
~MyClass();
const char* getMyVar1() { return m_myVar1; }
const char* getMyVar2() { return m_myVar2; }
bool operator < (const MyClass&);
bool operator > (const MyClass&);
bool operator <= (const MyClass&);
bool operator >= (const MyClass&);
bool operator == (const MyClass&);
};
-----------
#include <LIST>
#include "MyClass.h"
int main()
{
std::list<MyClass> myList;
MyClass o1;
MyClass o2;
myList.push_back(o1);
myList.push_back(o2);
myList.sort();
return 0;
}
-- modified at 5:18 Thursday 22nd December, 2005
|
|
|
|
|
you don't pass it, sort will use them !!!
it is mentioned on the link on the msdn site i provided previously that sort() (without parameter) will sort in the ascending order...
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VisualCalc 3.0]
-- modified at 5:13 Thursday 22nd December, 2005
|
|
|
|
|
I got compiler faults: "... public: bool __thiscall MyClass::operator<(class MyClass const &)" (??MCID3@@QAE_NABV0@@Z ...."
Do I have to define these methods anywhere????
// these are for comparison
bool operator < (const MyClass&);
bool operator > (const MyClass&);
bool operator <= (const MyClass&);
bool operator >= (const MyClass&);
bool operator == (const MyClass&);
Isn't it only the prototypes????
|
|
|
|
|
nne-vitamin wrote: Do I have to define these methods anywhere????
yes of course !!
in general, we declare a class in a .h file, and implement it in a .cpp.
myclass.h:
<font color=blue>class</font> MyClass {
<font color=blue>private</font>:
<font color=blue>char</font> m_myVar1[255];
<font color=blue>char</font> m_myVar2[255];
<font color=blue>public</font>:
MyClass();
~MyClass();
<font color=blue>const char</font>* getMyVar1();
<font color=blue>const char</font>* getMyVar2();
<font color=green>
<font color=blue>bool operator</font> < (<font color=blue>const</font> MyClass&);
<font color=blue>bool operator</font> > (<font color=blue>const</font> MyClass&);
<font color=blue>bool operator</font> <= (<font color=blue>const</font> MyClass&);
<font color=blue>bool operator</font> >= (<font color=blue>const</font> MyClass&);
<font color=blue>bool operator</font> == (<font color=blue>const</font> MyClass&);
};
myclass.cpp:
MyClass::MyClass() {
<font color=green>
}
MyClass::~MyClass() {
<font color=green>
}
<font color=blue>const char</font>* MyClass::getMyVar1() {
<font color=blue>return this</font>->myVar1;
}
<font color=blue>const char</font>* MyClass::getMyVar2() {
<font color=blue>return this</font>->myVar2;
}
<font color=blue>bool</font> MyClass::operator < (<font color=blue>const</font> MyClass& crefCl) {
<font color=green>
}
<font color=blue>bool</font> MyClass::operator > (<font color=blue>const</font> MyClass& crefCl) {
<font color=green>
}
<font color=blue>bool</font> MyClass::operator <= (<font color=blue>const</font> MyClass& crefCl) {
<font color=green>
}
<font color=blue>bool</font> MyClass::operator >= (<font color=blue>const</font> MyClass& crefCl) {
<font color=green>
}
<font color=blue>bool</font> MyClass::operator == (<font color=blue>const</font> MyClass& crefCl) {
<font color=green>
}
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VisualCalc 3.0]
-- modified at 6:35 Thursday 22nd December, 2005
|
|
|
|
|
Thank you very much! But I don't know what (the comparisons) I have to write to the definition of these classes operators?
bool MyClass::operator < (const MyClass& crefCl) { //do the comparison here...}
bool MyClass::operator > (const MyClass& crefCl) { //do the comparison here...}
bool MyClass::operator <= (const MyClass& crefCl) { //do the comparison here...}
bool MyClass::operator >= (const MyClass& crefCl) { //do the comparison here...}
bool MyClass::operator == (const MyClass& crefCl) { //do the comparison here...}
I only want my list to be sorted based on the content of m_myVar2. If the contents of m_myVar2 are:
"One"
"Two"
"Three"
"Four"
so after calling myList.sort() the contents should be alphabetically sorted like:
"Four"
"One"
"Three"
"Two"
How can I do that? If you write: //do the comparison here...
I cannot guess what.
-- modified at 6:44 Thursday 22nd December, 2005
|
|
|
|
|
damn !!! is it so hard ?!
<font color=blue>#include</font> <string.h>
<font color=blue>bool</font> MyClass::operator < (<font color=blue>const</font> MyClass& crefCl) {
<font color=blue>return</font> (::strcmp(m_myVar2, crefCl.GetVar2()) < 0);
}
<font color=blue>bool</font> MyClass::operator > (<font color=blue>const</font> MyClass& crefCl) {
<font color=blue>return</font> (::strcmp(m_myVar2, crefCl.GetVar2()) > 0);
}
<font color=blue>bool</font> MyClass::operator <= (<font color=blue>const</font> MyClass& crefCl) {
<font color=blue>return</font> (::strcmp(m_myVar2, crefCl.GetVar2()) <= 0);
}
<font color=blue>bool</font> MyClass::operator >= (<font color=blue>const</font> MyClass& crefCl) {
<font color=blue>return</font> (::strcmp(m_myVar2, crefCl.GetVar2()) >= 0);
}
<font color=blue>bool</font> MyClass::operator == (<font color=blue>const</font> MyClass& crefCl) {
<font color=blue>return</font> (::strcmp(m_myVar2, crefCl.GetVar2()) == 0);
}
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VisualCalc 3.0]
-- modified at 6:47 Thursday 22nd December, 2005
|
|
|
|
|
Thank you! The important secret of your hints came at last. So it was very hard! Even though thank you very much!
|
|
|
|
|
nne-vitamin wrote: The important secret of your hints came at last
and the important question still remains... : does it work ?
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VisualCalc 3.0]
|
|
|
|
|
Sorry, no.
The compiler fault: 'getMyVar2' : this-pointer cannot be converted from 'const class MyClass' to 'class MyClass &'
If I write:
bool MyClass::operator < (const MyClass& crefCl) { return (::strcmp(m_myVar2, crefCl.m_myVar2) < 0);}
the same fault
|
|
|
|
|
|
Thank you! Yes I am a newbie! Therefor I ask. Why do you make it so difficult to me? It is your Intention?
It is so easy to post a simple code instead to make a secret from that.
I find out the easy way myself bei Codeguru, only short way:
http://www.codeguru.com/forum/showthread.php?t=366063[^]
|
|
|
|
|
nne-vitamin wrote: Why do you make it so difficult to me?
2 things :
- because it is not difficult if you know the basics of C++ (which is apprently not the case)
- the example on codeguru is exactly what i suggested you, but unfortunately, you didn't apply it correctly. it's not my fault buddy. learning C++ was only a good advice, now, the balls are in your area ; you do what you want...
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VisualCalc 3.0]
|
|
|
|
|
FYI:
Most C++ experts recommend that you make operator<() a non-member function.
And that you create a operator<=() as a member function.
That paticular example in CodeGuru is not a good one, and it may be a little out date.
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
|
|
|
|
|
// We use this object so that list::sort() can sort in a different
// order.Normally greater<> is used. We override it here to switch to
// less.
class less_str : public greater<string> {
public:
bool operator()(const string& _X, const string& _Y) const
{
return _X <= _Y;
}
};
I have directly use the list.sort(less_str());
In this processing, I have trace into ...
but it always get into greater<string> operator (), why it don't get into less_str operator()?
|
|
|
|
|
|
If you use VC++ 6.0 it's probably a bug in VC++ 6.0. See MSDN
|
|
|
|
|
Dear Members,
I need to color a button using ATL, WTL or WIN 32 API..
I can easily do it using MFC but because of the Application requirement
I have to do it by using the above mentioned technologies only.
Kindly help me out by sending tips and useful codes.
Regards,
Mayank Srivastava
|
|
|
|
|
srimayank11_codeproject wrote: I need to color a button using ATL, WTL or WIN 32 API..I can easily do it using MFC but because of the Application requirementI have to do it by using the above mentioned technologies only.
Here's a good start with WTL:
class CColorButton : public CWindowImpl<CColorButton, CButton> {
public:
DECLARE_WND_SUPERCLASS(_T("CColorButton"), _T("BUTTON"))
BEGIN_MSG_MAP(CColorButton)
MSG_OCM_DRAWITEM(OnDrawItem)
END_MSG_MAP()
public:
void OnDrawItem(UINT uCode, LPDRAWITEMSTRUCT pdis) {
}
};
Please note that you will have to make your button BS_OWNERDRAW (window style flag). This unfortunately makes the button unable to work properly as a default button. If you need this functionality, I can provide that code as well. I just wanted to make this code as simple and easy to understand as possible, as it illustrates how to draw the button yourself easily. Also, please do not forget to add REFLECT_NOTIFICATIONS() in your parents message handler. If you don't, your button will not be drawn.. at all!
--
Pictures[^] from my Japan trip.
|
|
|
|
|
As many of you probably know, Visual Studio 2005 contains a nasty std::iostream bug that causes a memory leak. If, like me, you use streams such as stringstream/wstringstream, fstream, etc. you will be affected. The fix is to rebuild the MSVCRT80.DLL, which is a major undertaking and, IMHO, not worth the pain.
Hence I thought I'd investigate STLPort. I have heard good things about this library, and it would allow me to work around the iostream bug, as I wouldn't be using the MS implementation.
However, I also use Boost, and there doesn't seem to be a config for VS 2005 _and_ STLPort! Sob. Has anyone here made the three work together?
The Rob Blog Google Talk: robert.caldecott
|
|
|
|
|
Robert Edward Caldecott wrote: The fix is to rebuild the MSVCRT80.DLL, which is a major undertaking and, IMHO, not worth the pain.
It's also a pain because you'd have to put the DLL next to your binary. You never know which app will overwrite that file. So in essence, it'd be like using a static library, but with a bigger startup overhead!
Robert Edward Caldecott wrote: However, I also use Boost, and there doesn't seem to be a config for VS 2005 _and_ STLPort! Sob. Has anyone here made the three work together?
I don't see how boost comes into the picture..? IIRC, STLPort is supposed to be a drop in replacement for the regular STL library that ships with Visual Studio. As for the missing config for VS 2k5, have you tried using the 2k3 config? It might work straight out of the box...
--
Pictures[^] from my Japan trip.
|
|
|
|
|
Jörgen Sigvardsson wrote: I don't see how boost comes into the picture..?
Boost has different jam configs when building for VC + STLPort, so I think it must be relevanr somehow...
The Rob Blog Google Talk: robert.caldecott
|
|
|
|
|
Jörgen Sigvardsson wrote: It's also a pain because you'd have to put the DLL next to your binary. You never know which app will overwrite that file.
What do you mean by this?
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
If you rebuild the crt dll, and install it in say Windows\System32, it could be overwritten by another program. Overwritten with the "original" crt dll which has the memory leaks.
If you put the dll alongside the binary, the chances of another application overwriting it is virtually zero.
--
Pictures[^] from my Japan trip.
|
|
|
|
|
Jörgen Sigvardsson wrote: If you rebuild the crt dll, and install it in say Windows\System32,
Shudder. Never even cross my mind to install anything in System32, but you are right - many people actually do that
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Uh huh. I've stopped doing that as well. It used to be "correct practice" once I believe, but it's just asking for headaches. I do DLLs only these days, but I put them side by side with the executable. It won't be much better than .libs from an optimization point of view (shared code, etc). But it is easier from a maintainence point of view, as I only have to patch affected DLLs, and not the entire app.
Another great benefit with DLLs is that it's very easy to debug code on site, as it's just a matter of replacing the DLLs which contains TRACE messages, and/or similar debugging I/O.
I like DLLs. Does it show?
--
Pictures[^] from my Japan trip.
|
|
|
|