0
我想比较我的类CustomerNode的两个对象,然后返回与这些方法的字母优先级相关的结果。我无法弄清楚为什么这不起作用。对我而言,逻辑看起来不错,我遵循我作业中的指示。重载的比较运算符不工作
bool OrderedList::add (CustomerNode* newEntry)
{
if (newEntry != 0)
{
CustomerNode * current;
CustomerNode * previous = NULL;
if(!head)
head = newEntry;
current = head;
// initialize "current" & "previous" pointers for list traversal
while(current && *newEntry < *current) // location not yet found (use short-circuit evaluation)
{
// move on to next location to check
previous = current;
current = current->getNext();
}
// insert node at found location (2 cases: at head or not at head)
//if previous did not acquire a value, then the newEntry was
//superior to the first in the list.
if(previous == NULL)
head = newEntry;
else
{
previous->setNext(newEntry); //Previous now needs to point to the newEntry
newEntry->setNext(current); //and the newEntry points to the value stored in current.
}
}
return newEntry != 0; // success or failure
}
好了,这是包含在程序中的重载操作<,测试它的通用对象轮番上涨的预期成果:
bool CustomerNode::operator< (const CustomerNode& op2) const
{
bool result = true;
//Variable to carry & return result
//Initialize to true, and then:
if (strcmp(op2.lastName, lastName))
result = false;
return result;
}
我很新的节目,让我明白任何回应,特别是风格评论,因为我仍然在学习。我的逻辑错了吗,还是有其他规则需要注意?我不完全理解为什么我的参数是引用,或者如果我实际上需要取消引用参数来调用操作符。
谢谢。
我认为你需要更正参数的顺序到strcmp为好,应是'strcmp(lastName,op2.lastName)' – usta
谢谢!我认为这实际上是第二次帮助我弄清楚我在做这个程序时做错了什么,你是一个救生员,Luchian。 – mr0il