Sign in to follow this  
barakat

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;
}
any help please !!!
edited by evolutional to aid code formatting [Edited by - evolutional on December 1, 2004 4:17:19 AM]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this