Possibly Noob help needed on Overload Functions

Posted By: EvilSOB

Possibly Noob help needed on Overload Functions - 07/16/08 11:32

Hi all, can someone please point out what Im doing wrong here.

Ive read overloaded functions work fine, and I think Ive got
it right according to the manual, so what am I doing wrong?

If I use ANY but the first declared function, I get error
Quote:
'PopupMenu'. Wrong number/type of parameters

at compile time.

Code:
//Declares/Prototypes
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer);  //Want to keep this "default" if I can
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y);
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y, STRING* font);

//Usage  (FAILS unless I kill the first two prototypes and stick with the third)
PANEL* tmpPanel;
...
PopupMenu(tmpPanel,"Dummy_string_to_test_funtion",100,10,15,"Times New Roman#24b");
...

//Actual Functions
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer)
{   PopupMenu(ReturnPanel, SourceData, layer, 0, 0, ,"Times New Roman#12b");  }
//
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y)
{   PopupMenu(ReturnPanel, SourceData, layer, pos_x, pos_y, ,"Times New Roman#12b");   }
////
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y, STRING* font) 
{   ...performed code (eventually) by all overloads...   }


PS Ive tried with functions as VOID and FUNCTION and INT.

Thanks in advance

Posted By: Joozey

Re: Possibly Noob help needed on Overload Functions - 07/16/08 14:16

Quote:

If I use ANY but the first declared function, I get error
Quote:
'PopMenu'. Wrong number/type of parameters

at compile time.

PopMenu or PopupMenu?

What is the line of code that causes the error?
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 07/17/08 02:09

"PopupMenu" sorry, typo in posting. (will be edited)

PopMenu is a global panel pointer being used by the calling function
(which I'll also edit into original post)

Ive changed both the "PopMenu" pointer to "tmpPanel" in the code and changed the
string parameter contents to dummy data, to help remove possibly confusing looking names.

Hope this helps, and thanks for looking.
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 07/21/08 08:15

Still no luck, tried puuting my functions in an include
and dropped the prototyping and no change, both as a
'.c" and a ".h" include.

Still no ideas from anyone else? This is really bugging me.
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 09/06/08 12:15

"bump"

Any fresh eyes have any ideas?

Or should I try dropping this issue into the "Ask Conitec" forum?
Posted By: Trooper119

Re: Possibly Noob help needed on Overload Functions - 09/06/08 14:50

Well most of your code looks pretty good, I think your looking to far into it though. It is possible your values your inputting it may be read as int's, double's, float's etc. To avoid this you should always define your variables you put into the functions you call.

ex:
Code:
var numA = 10;
var numB = 40;
var numC = 85;
STRING* charA, charB;
charA = "A message of sorts";
charB = "Another message";
//Then put those in the function


Otherwise make more functions with more combinations, it is also possible that your strings are being read as char's but I don't think that's likely.
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 09/06/08 17:11

Sorry Troop, but its failing at compile time, so values are immaterial.

To my way (possibly flawed) of thinking, the fact that my different overloads
have a different quantity of parameters should force through any type-cast issues.

This is the case in C.net anyway, and I think it was in C++ too.
Posted By: Trooper119

Re: Possibly Noob help needed on Overload Functions - 09/06/08 17:50

To overload a function in C++ you have the same name but different variables (this can be different variables as in an int instead of double) or a different number of variables present. Any combination of these two works as well. If the error is correct (not holding my breath) it means your inputting the wrong variables compared to what is expected.

EDIT: Please note if the error is what I said it is, it would fail at compile time, if it is within the game, then the error would possibly occur when you first try to use the function, assuming at least one of those functions work.

My original suggestion I believe is still valid (unless you tried it already) please humor me and create a function that accepts doubles and ints and if they don't work, the error is incorrect. Then you'll have to look at other options.

In response to an earlier comment of yours, I'm not sure what function does but putting void, int, double etc. in front of your function simply tells the function that you are returning that kind of variable within the function. Unless you are returning something I suggest you stick with function or void. But you previously said you've tried both options so this is just an FYI to anyone who is confused on the matter.

EDIT: What call works and what call doesn't by the way? I'm curious.

EDITx2: The reason it may be bugging out on you is when you simply input 15 or "a message" into a function, the compiler is up to its own devices to figure out what that is, just because you want it to be a var doesn't mean it will be passed as such (in this case it should be an int I believe)
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 09/06/08 19:50

I tried changing to ints and it didnt make any difference, still fails on compile, "Wrong number/type of parameters".
ALL the overloads have a different NUMBER of parameters, and this number of parameters is unique to each overload variant, as in
the example in my original post. So shouldnt this 'number of parameters' variation override any type-casting issues?

FYI : I get the same problem whether I use prototypes for my overloaded functions or not.

What work and what doesnt is as follows, using these prototypes
Code:
//Prototypes
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer);
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y);
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y, STRING* font);
All compiles OK as long as I only call PopupMenu with three parameters (ie first overload).
Any other number of parameters and I get the compile error
Quote:
'PopupMenu'. Wrong number/type of parameters

But if I change the order to say
Code:
//Prototypes
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y);
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y, STRING* font);
void PopupMenu(PANEL* *ReturnPanel, STRING* SourceData, var layer);
All NOW compiles OK as long as I only call PopupMenu with FIVE parameters (ie new first overload).

Any ideas?


PS again please remember I get the same problem whether I use prototypes for my overloaded functions or not.
Posted By: DJBMASTER

Re: Possibly Noob help needed on Overload Functions - 09/06/08 20:28

hmmmm maybe is has something to do with you sending char* datatypes instead of string*.

manual...

Code:
When the argument types don't match any of the overloaded functions, the first function is automatically used! 
This can happen when it expects a STRING* and a char* is passed. 
This is important when passing constants to overloaded functions: Text constants like "this is a text" are of type char*,not STRING*. 


so when you use...

PopupMenu(tmpPanel,"Dummy_string_to_test_funtion",100,10,15,"Times New Roman#24b");

wouldn't "Dummy_string_to_test_funtion" and "Times New Roman#24b" be passed as char* and not string*, causing a crash?

Not sure if this is relevant, but its worth a try.

If you have tried using ints and it still isn't working then your function must be wrong...

Try and compile this piece of code...

Code:
#include <acknex.h>
#include <default.c>
var c = 0;

function hello(int i, int j)
{
	for(c=0; c<(i+j); c++)
	{
		beep();
		wait(-1);
	}
	
}

function hello(int i, int j, int k)
{
	
	for(c=0; c<(i+j+k); c++)
	{
		beep();
		wait(-1);
	}
	
}

function hello(int i, int j, int k, int l)
{
	for(c=0; c<(i+j+k+l); c++)
	{
		beep();
		wait(-1);
	}
	
}

void main()
{
hello(3,5,2);
wait(-15);
hello(2,3,4,1);
}

Posted By: testDummy

Re: Possibly Noob help needed on Overload Functions - 09/06/08 20:53

Quoting DJBMASTER.
Quote:
Not sure if this is relevant, but its worth a try.

Seems relevant from over here.
Nicely done, if it is.


Guesses:
If Lite-C has a quick STRING* conversion macro (i.e. string() ), 'you' might trying passing char* constants wrapped in that macro.
Alternately, you might try to write additional overloaded functions which accept char* constants instead of STRING*.
Wait, is there an additional * in each function prototype / definition!?
Should ReturnPanel be a pointer to a pointer!?
tmpPanel doesn't look like a pointer to a pointer from over here.


Code:
//Declares/Prototypes (new)
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, var layer);  //Want to keep this "default" if I can
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y);
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, var layer, var pos_x, var pos_y, STRING* font);
void PopupMenu(PANEL* ReturnPanel, char* SourceData, var layer);
void PopupMenu(PANEL* ReturnPanel, char* SourceData, var layer, var pos_x, var pos_y);
void PopupMenu(PANEL* ReturnPanel, char* SourceData, var layer, var pos_x, var pos_y, char* font);


Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 09/07/08 13:20

Hmm, interesting..... (bing! shocked!)
The snippet you sent works fine, but for some reason Im now thinking of this problem a different way...

Multiple times in this thread, people have been finicky about my typecasting, and Ive been shrugging
them off as I (mistakenly) believed that the Number-of-params would override typecasting.
IE I thought that typecasting is only important IF number of parameters MATCH.

Now, after everyones input, Im thinking that the overload will only be run
if BOTH type-casts AND Number-of-params Match, if theres no PERFECT match, then first
function/prototype is executed, AND if its Number-of-params is wrong, will generate a compile error.


So I NOW need to be picky about my type-cast BEFORE compile times. I normally leave
typecasting sloppy and fix then once the compile succeeds, but thats been screwing me here.

I'll be doing tests on this shortly, and I thank ALL who have contributed to this thread,
even if I did not take your advice before, it has finally gotten through my thick skin/Ego.
blush
Posted By: EvilSOB

Re: Possibly Noob help needed on Overload Functions - 09/07/08 14:54

Finally, thanks to everyones help, Ive got it.
Here are the working prototypes for the functions.
Code:
//Declares/Prototypes
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, int layer);  
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, int layer, int pos_x, int pos_y);
void PopupMenu(PANEL* ReturnPanel, STRING* SourceData, int layer, int pos_x, int pos_y, STRING* font);
and I dont need to put any typecasting in the calls except for _str() to use char constants.
Code:
PANEL* NewPanel=NULL;
...
PopupMenu(NewPanel, _str("SendThisAsString"), 1, 100, 200);
...  Gives me 
......'ReturnPanel' is a pointer to NewPanel, 
......'SourceData'  is a string containing "SendThisAsString", 
......'layer'       in an int containing '1', 
......'x_pos'       in an int containing '100', 
......'y_pos'       in an int containing '200'
Exactly what I wanted, and I now understand fully what I was doing wrong.

Another couple of things I leaned today were
1> if you pass a char constant parameter (eg func("xx");) it is converted to string automatically,
BUT GETS TREATED AS A CHAR ARRAY for deciding which overload to run.
2> Numeric constant parameters default to "int" in the call (ie func(15); == func((int)15);)
but the end function MUST specify that it is expecting an "int". ie func( int InValue ) {...}

I hope my problems here are of help to someone else.
And once again, thanks to ALL who helped me over this mental block.
© 2024 lite-C Forums