P
puzzlecracker
I need to be able to map two string to some value in the map. I
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it's more descriptive. However, in the latter
implementation (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.
#include<map>
#include <string>
using namespace std;
struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){}
string a_;
string b_;
};
inline bool operator <(const MyClass &a, const MyClass & b)
{
return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);
}
int main()
{
//This caused the error
map<MyClass, MyClass *>m;
MyClass my("Foo", "Bar");
m.find(my);
//This fixed it
map<MyClass, MyClass *>m1;
m1.find(MyClass("Foo","Bar"));
}
Unfortunately, I can't show the compiler error, b/c I couldn't
reproduce it at home, only at work. Perhaps, error I'll post it
tomorrow.
I want to see if anyone can see the issue here or suggest an
alternative design.
Thanks
consider two alternatives to implement : using pair class or using a
separate class/struct containing two member strings. I prefer the
latter for it's more descriptive. However, in the latter
implementation (equivalent of which is shown below), I got some
compiler errors on gcc 3.3.1 with MyClass.
#include<map>
#include <string>
using namespace std;
struct MyClass
{
MyClass ( const string &a, const string & b):a_(a),b_(b){}
string a_;
string b_;
};
inline bool operator <(const MyClass &a, const MyClass & b)
{
return a.a_<b.a_ ||(a.a_==b.a_?a.b_<b.b_:false);
}
int main()
{
//This caused the error
map<MyClass, MyClass *>m;
MyClass my("Foo", "Bar");
m.find(my);
//This fixed it
map<MyClass, MyClass *>m1;
m1.find(MyClass("Foo","Bar"));
}
Unfortunately, I can't show the compiler error, b/c I couldn't
reproduce it at home, only at work. Perhaps, error I'll post it
tomorrow.
I want to see if anyone can see the issue here or suggest an
alternative design.
Thanks