Call a procedure with pointer as argument

Started by
5 comments, last by frob 7 years, 11 months ago

Hey guys, how are you ?

Now, i'm studying C++ and i got one question !

I have this function :


AddWeapon( sInven_Set* pItem )

and i need to call this on other function, but i dont know how to init sInven_Set* pItem.

If i init and just call pItem with null value game crashs because this ASSERT


	if( pItem->nClassNum == WMGR_INVALID_ID ) 
	{
		ASSERT(!"pItem->nClassNum ==255");
		return LTFALSE;
	}

so , how to solve that ?

Thanks a lot !

Advertisement

You have to declare the item before you reference it. I'm not sure about the rest of your code, but you should have something like this:


sInven_Set MySword;
........
........ whatever code.....

//call the "addweapon" function
AddWeapon(&MySword);//this is a reference for "MySword" which is of data type "sInven_Set"

When you actually want to have a variable that holds info, you will have to initialize it. And then if you want to send it a pointer to it within a function, you use "&" to reference it. Your function declaration will look the same as you have it above, but when you call it you will use an actual initialized object's reference.

Please give more code if I'm in error about how you are calling the function......

ASSERT(!"pItem->nClassNum ==255");

You try to check if the string "pItem->nClassNum ==255" is null. The ! operator is called with a pointer (const char*) pointing to the string as argument. This pointer is not null, so ! returns false. I don't think this is intended, however, you could simply write assert(false) to get the same behaviour.

Hawblood, i made it like you sayd and works. Game do not crash. Thanks man !

Glad to help.

ASSERT(!"pItem->nClassNum ==255");

You try to check if the string "pItem->nClassNum ==255" is null. The ! operator is called with a pointer (const char*) pointing to the string as argument. This pointer is not null, so ! returns false. I don't think this is intended, however, you could simply write assert(false) to get the same behaviour.



This is a common pattern used to let you specify an explanation for the assert rather than just "false." Many code bases instead use a second parameter to ASSERT() or their own variant that takes a string message.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You can add it to the built-in if the string option doesn't exist thanks to the design of the assert macro.

assert( foo==bar && "message goes here" );

The contents of the assert macro are tested and if it stops it also dumps the contents inside the parenthesis. The second half of && "message" does not effect the overall truth of the statement, if false it remains false because the first half fails, if it is true it remains true because && string is true.

So when it crashes you get something like:

Assertion failed! somefile.cpp:37: foo==bar && "message goes here"

Which is very useful for the error message.

More advanced systems will want to build a custom skippable assert, something that doesn't crash when an assert happens, write code that attempts to provide a sane default after an assertion, have assertion systems record to log files with the severity of the error condition and what triggered it and hopefully how to correct it, etc. But those are more advanced. When you've got a simple solution, the basic assertion && message is a good pattern to use.

This topic is closed to new replies.

Advertisement