结构中带有类的链表无法使布尔操作正常工作
我似乎无法让 Boolean
操作像我想象的那样工作。
/////.h file
class linkD
{
private:
struct ListNode{
driver driver; ////class
ListNode *next;
};
ListNode *head;
///.cpp file
void linkD::deleteNode(driver d){
ListNode *nodePtr;
ListNode *previousNode;
ListNode *newNode;
newNode=new ListNode;
newNode->next=NULL;
newNode->driver=d;
if(!head)
return;
if(head->driver==d) //the problem is right here.
{
nodePtr=head->next;
delete head;
head=nodePtr;
}
head->driver==d
给出了一条红线(没有运算符“==”与这些操作数匹配)
我认为这是因为 head->driver< /code> 未初始化,但我可能是错的,我不确定如何初始化它,因为它位于未初始化的结构内。
I can't seem to get Boolean
operation working like I thought it works.
/////.h file
class linkD
{
private:
struct ListNode{
driver driver; ////class
ListNode *next;
};
ListNode *head;
///.cpp file
void linkD::deleteNode(driver d){
ListNode *nodePtr;
ListNode *previousNode;
ListNode *newNode;
newNode=new ListNode;
newNode->next=NULL;
newNode->driver=d;
if(!head)
return;
if(head->driver==d) //the problem is right here.
{
nodePtr=head->next;
delete head;
head=nodePtr;
}
head->driver==d
gives a redline (no operator "==" matches these operands)
I think it is because head->driver
is uninitialized but I might be wrong and I am not sure how to initialize it since it's inside a uninitialized struct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为 driver 是一个类对象。
您必须为类定义相等运算符。
您是否期望类对象像指针一样(如在 Java 中,即它们可以为 NULL)
如果您想测试驱动程序对象的相等性,请像这样定义它:
That's because driver is a class object.
You have to define the equality operator for a class.
Are you expecting class object to be pointer like (as in Java i.e. They can be NULL)
If you want to test driver objects for equality define it like this:
代码中的类/结构驱动程序似乎没有定义运算符 ==() ,因此编译器会抱怨。
The
class/struct driver
in your code doesn't seem to have defined theoperator ==()
and because of that the compiler is complaining.