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.
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:
class driver
{
public:
bool operator==(driver const& rhs) const
{
return x == rhs.x && y == rhs.y && z == rhs.z;
}
private:
int x;
int y;
int z;
};
int test
{
driver x;
driver y;
if (x == y) // this calls x.operator==(y)
{ // So y is the rhs parameter.
}
}
The class/struct driver
in your code doesn't seem to have defined the operator ==()
and because of that the compiler is complaining.
精彩评论