This question already has an answer here:
I have been google all over the place and am unable to find a working example for how to use std::set and control the insert order. I found comments for my type of structure but I get a compile error.
I am trying to sort on myMaskName so I get
address class
0x19ec220 TEST1 2 26
0x19eba90 TEST2 100 26
0x19ebb00 TEST3 230 26
0x19eba20 TEST4 240 26
vs. this default order
0x19eba20 TEST4 240 26
0x19eba90 TEST2 100 26
0x19ebb00 TEST3 230 26
0x19ec220 TEST1 2 26
This is my test code:
class FoIxCompareMaskNew
{
public:
// Main constructor.
FoIxCompareMaskNew (const char *maskName, const int& startAddress, const int& numWords) :
myMaskName(maskName),
myStartAddress(startAddress),
myNumberWords(numWords)
{
}
~FoIxCompareMaskNew ();
std::string MaskName() {return myMaskName;}
int StartAddress() {return myStartAddress;}
int NumberWords() {return myNumberWords;}
private:
std::string myMaskName;
int myStartAddress;
int myNumberWords;
};
struct FoIxCompareMaskNewComp {
bool operator()(const FoIxCompareMaskNew* p_lhs, const FoIxCompareMaskNew* p_rhs) const
{
std::string temp = p_lhs->MaskName();
return temp.compare(p_rhs->MaskName());
}
};
void newWay()
{
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList;
std::multiset<FoIxCompareMaskNew *, FoIxCompareMaskNewComp> maskList2;
FoIxCompareMaskNew * list[4];
list[0] = new FoIxCompareMaskNew("TEST1", 2, 26);
list[2] = new FoIxCompareMaskNew("TEST3", 230, 26);
list[1] = new FoIxCompareMaskNew("TEST2", 100, 26);
list[3] = new FoIxCompareMaskNew("TEST4", 240, 26);
maskList.insert(list[0] );
maskList.insert(list[1] );
maskList2.insert(list[0] );
maskList2.insert(list[1] );
maskList2.insert(list[2] );
maskList2.insert(list[3] );
}
int main(int, char**)
{
newWay();
}
This is the error I see on compile. It doesn't like me accessing the members.
./main.C: In member function ‘bool FoIxCompareMaskNewComp::operator()(const FoIxCompareMaskNew*, const FoIxCompareMaskNew*) const’:
./main.C:205:38: error: passing ‘const FoIxCompareMaskNew’ as ‘this’ argument of ‘std::string FoIxCompareMaskNew::MaskName()’ discards qualifiers [-fpermissive]
std::string temp = p_lhs->MaskName();
^
./main.C:206:39: error: passing ‘const FoIxCompareMaskNew’ as ‘this’ argument of ‘std::string FoIxCompareMaskNew::MaskName()’ discards qualifiers [-fpermissive]
return temp.compare(p_rhs->MaskName());
All the examples I have founder are for int or std::string. I have found no example of someone using a class like this beyond saying setup a compare struct and use that.
So what am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire