# WHAT IS WRONG WITH THIS ??

## Recommended Posts

this code is generating errors ... because the compiler cant evaluate any expressions because the this pointer == NULL :'(
bool node::operator<(const node & other)
{
assert(this);//OH MY GOD THAT FAILED
switch(sortMethod){
case byID:
return (ID<other.ID);
break;
case byName:
return (strcmp(fName.c_str() , other.fName.c_str())<0 );
break;
case byTotals:
//canot be evaluated
return ((cWg+fEg)<(other.cWg+other.fEg));
break;
default:
assert(0);
break;
}

assert(0);
return false;
}

edited by evolutional to aid code formatting [Edited by - evolutional on December 1, 2004 4:17:19 AM]

##### Share on other sites
We also need the calling code. And please format your code correctly.

##### Share on other sites
Member functions are nothing more then ordinary functions with an extra parameter : the this pointer. So calling a method on a object which is NULL is perfectly OK, and won't even crash if you don't touch the objects variables (and any virtual functions).

You're method isn't the problem, it's where you call the method. The object you're calling the method on is NULL. You need to make sure that the object isn't NULL.

##### Share on other sites
Perhaps the problem is where you are calling the operator, rather then the operator itself.

If the lhs might possibly be null, I guess the assert(this) would fail - though you would expect it to fail before that.

Would be useful if you gave snippets of use.

##### Share on other sites
okay i have class list....

const list * list::sortByTotal()
{

if(isEmpty())
return 0;

list * sortedByTotalsList = new list;

node::setSortMethod(byTotals);

node * current = first ;
while(current){
sortedByTotalsList->insert(current->ID , current->fName , current->lName ,current->cWg ,current->fEg );
current = current->next;
}

return sortedByTotalsList;

}

##### Share on other sites
paulecoyote u r very right

the very silly error was in the insert function of the list itself :\$

//i added the (current->next ) i was comparing current and current->next without checking current->next!!

while(current && current->next && (*(current->next) < *newNode) )
current = current->next;

##### Share on other sites
Dont think it was just me whom said that, but thanks for posting you resolved it. Sometimes these things just go unanswered ;-)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627698
• Total Posts
2978681

• 20
• 14
• 12
• 10
• 12