1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
If Person is the first arg, I get compiler error. If it is string or any primitive type, it works. Complete example is pasted below.
Thank you in advance.
Regards,
Saleem
====> map <Person, PhoneNum> PhoneBook0;
PhoneBook0.insert(pair< Person, PhoneNum>(Penny, PennysNum));
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
class Person {
char name[40];
public:
Person() {
strcpy_s(name,2,"");
}
Person(char *name) {
strcpy_s(this->name, strlen(name)+1, name);
}
char * getName() {return this->name;}
//bool operator <(const char *);
//bool operator ==(const char *);
bool operator <(Person &);
bool operator ==(const Person &);
};
//bool Person::operator <(const char * name) {
bool Person::operator <(Person &OtherPerson) {
return (strcmp(this->name, OtherPerson.name) < 0) ;
}
bool Person::operator ==(const Person &OtherPerson) {
return (strcmp(this->name, OtherPerson.name) == 0);
}
class PhoneNum {
char num[40];
public:
PhoneNum() {
strcpy_s(num, 2, "");
}
PhoneNum(char *n ) {
strcpy_s(num, strlen(n)+1, n);
}
char * getNum() {return this->num;}
bool operator <(const char*);
bool operator ==(const char*);
};
bool PhoneNum::operator <(const char * otherNum) {
return (strcmp(this->num, otherNum) < 0);
}
bool PhoneNum::operator ==(const char * otherNum) {
return (strcmp(this->num, otherNum) == 0);
}
void main() {
Person Peter("Peter Piper");
Person Penny("Penny Lane");
PhoneNum PetersNum("603-555-1212");
PhoneNum PennysNum("603-555-1213");
cout << "Peter's name is " << (Peter.getName()) << " and his number is " << PetersNum.getNum() << endl;
map <char *, char *> myList;
myList.insert(pair<char *, char *>(Peter.getName(), PetersNum.getNum()));
map <Person, PhoneNum> PhoneBook0;
PhoneBook0.insert(pair< Person, PhoneNum>(Penny, PennysNum));
//map <Person, PhoneNum> PhoneBook;
map <char*, PhoneNum> PhoneBook;
PhoneBook.insert(pair< char*, PhoneNum>("Peter", PetersNum));
map <char*, PhoneNum>::iterator p;
p = PhoneBook.find("Peter");
if (p != PhoneBook.end())
cout << "Peter's number is " << p->second.getNum() << endl;
else
cout << "Peter's number is not in the phonebook" << endl;
return;
}
|