Arguments that are not used

B

bg_ie

Lets say I have the following function -

int mouseclick(int x, in y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

Thanks,

Barry.
 
T

Thomas Tutone

Lets say I have the following function -

int mouseclick(int x, in y)

change the above line to:

int mouseclick(int x, int)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

Make the above change and the warning should disappear.

Best regards,

Tom
 
H

Howard

Lets say I have the following function -

int mouseclick(int x, in y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

One way is to change the function (in the .cpp file) so that its signature
is just

int mouseclick( int, int )
{
...
}

Another is to "use" those values, but don't do anything with them, like
this:

int mouseclick( int x, int y )
{
x;
y;
...
}

Another (at least in some compilers) is to use a #pragma:

int mouseclick( int x, int y )
{
#pragma unused x
#pragma unused y
...
}

(I could have that syntax wrong. Since it's a pragma, it's probably not the
most portable solution, anyway.)

-Howard
 
H

Howard

Howard said:
One way is to change the function (in the .cpp file) so that its signature
is just

int mouseclick( int, int )
{
...
}

Another is to "use" those values, but don't do anything with them, like
this:

int mouseclick( int x, int y )
{
x;
y;
...
}

Another (at least in some compilers) is to use a #pragma:

int mouseclick( int x, int y )
{
#pragma unused x
#pragma unused y
...
}

(I could have that syntax wrong. Since it's a pragma, it's probably not
the most portable solution, anyway.)

Oh, I just noticed you only wanted to ignore the y. Well, you get the
idea... :)
-H
 
M

Marcus Kwok

Lets say I have the following function -

int mouseclick(int x, in y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

I just comment the variable name in the function header:

int mouseclick(int x, in /*y*/)
{
// implementation
}
 
G

Gavin Deane

Lets say I have the following function -

int mouseclick(int x, in y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

The compiler is allowed to warn about anything it likes. The people who
wrote your compiler clearly decided that they'd like to warn you about
this particular situation. They also almost certainly provided you with
a way to get rid of that warning if you want to. Your options will most
likely be at least some of, in increasing order of preference IMO:

Leave the warning there but ignore it (worst option).
Turn down the global warning level of the compiler until this warning
goes away.
Turn off this particular warning in the compiler.
Turn off this particular warning for that translation unit only.
Turn off this particular warning for that piece of code only.
Rewrite the code (best option if possible and applicable).

For details of how to manage warning sensitivity in your particular
compiler, consult its documentation or a forum dedicated to that
compiler.

Gavin Deane
 
F

Frederick Gotham

posted:
Lets say I have the following function -

int mouseclick(int x, int y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?


If you have a very simple function, simply change it to:

int mouseclick(int x,int)
{

}

If, however, you've a larger function, one whose parameter list you don't
want to change, then try this:

int mouseclick(int x,int y)
{
(void)y;
}

The cast to void makes your intention clear to the compiler.
 
G

Greg Comeau

Lets say I have the following function -

int mouseclick(int x, in y)
{
cout << "My x position"<<x;
}

I get a warning that I am not using y. What can I do to get rid of this
warning without changing the function prototype?

You could not give the parameter a name, but some compiler
may still emit a diagnostic in such a case. You might also
be able to get away with in this case perhaps uses such as
'y = y;' or '(void)y;' but of course if possible get rid
of the parameter.
 
B

bg_ie

They also almost certainly provided you with
a way to get rid of that warning if you want to.

Thanks for your help guys. I think that its good that the compiler
warns you and I think its better not to turn of the option. Of course,
in some situations, you don't need to use all the params so its nice to
know how to avoid the compiler warning for these situations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top