【发布时间】:2010-05-04 18:58:02
【问题描述】:
我有这个:
typedef string domanin_name;
然后,我尝试以这种方式重载运算符
bool operator<(const domain_name & left, const domain_name & right){
int pos_label_left = left.find_last_of('.');
int pos_label_right = right.find_last_of('.');
string label_left = left.substr(pos_label_left);
string label_right = right.substr(pos_label_right);
int last_pos_label_left=0, last_pos_label_right=0;
while(pos_label_left!=string::npos && pos_label_right!=string::npos){
if(label_left<label_right) return true;
else if(label_left>label_right) return false;
else{
last_pos_label_left = pos_label_left;
last_pos_label_right = pos_label_right;
pos_label_left = left.find_last_of('.', last_pos_label_left);
pos_label_right = right.find_last_of('.', last_pos_label_left);
label_left = left.substr(pos_label_left, last_pos_label_left);
label_right = right.substr(pos_label_right, last_pos_label_right);
}
}
}
我知道重载运算符
问题是它在这一行进入了一个无限循环:
if(label_left<label_right) return true;
它似乎是在尝试使用这个重载函数本身来进行比较,但是 label_left 是一个字符串,而不是一个域名!
有什么建议吗?
【问题讨论】: