 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri May 05, 2006 9:21 am Post subject: pointers to functions |
|
|
Hello all,
I need some help. I've been learning C++ using the tutorial at
Cplusplus.com (http://www.cplusplus.com/doc/tutorial/pointers.html is
the page in question). Near the bottom of the section on pointers,
there is a code sample which is supposed to be demonstrating using
pointers to point to functions. It didn't seem to make sense, so I
tried compiling it; it wouldn't compile! Since the author of the
tutorial didn't leave any contact information, I'm turning to you
folks. Can anyone correct the following code for me?
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
Thanks for your help,
Danny
p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sat May 06, 2006 2:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com ha scritto:
| Quote: |
int minus (int,int) = subtraction;
|
This line is illegal. The syntax would suggest that minus is a function,
but it's initialized as it were a function pointer. As the intent of the
tutorial is to show function pointers, it should have been:
int (*minus) (int,int) = subtraction;
However, notice that in this case you might get an error in line:
| Quote: | n = operation (20, m, minus);
|
because there is a class named minus in namespace std and the "using
namespace std" directive is in effect, thus minus is an ambiguous
symbol. I said "might" because the symbol is defined in the standard
header <functional> but you are not directly including it. In that case,
you have to manually resolve the ambiguity, for example with:
n = operation (20, m, ::minus);
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Sat May 06, 2006 3:21 pm Post subject: Re: pointers to functions |
|
|
| Quote: | #include <iostream
using namespace std;
int addition (int a, int b)
{ return (a+b); }
|
If you're a beginner, I suggest you get into the habit of using const
EVERYWHERE you can:
int addition( int const a, int const b ) { return a + b; }
Or perhaps:
int addition( int a, int const b ) { return a += b; }
| Quote: | int minus (int,int) = subtraction;
|
That line is broken. Should be:
int (*minus)(int,int) = subtraction;
| Quote: | int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
|
It looks like that function was written by a retarded parrot.
int operation( int const x, int const y, int (*Func)(int,int) )
{
return Func(x,y);
//or, if you prefer:
return (*Func)(x,y);
}
| Quote: | p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
|
An accurate description.
-Tomás
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Hopkin Guest
|
Posted: Sat May 06, 2006 3:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | Can anyone correct the following code for me?
|
The main error is that 'minus' is supposed to be a *pointer* to a
function, so it needs to be declared as such:
int (*minus)(int,int) = subtraction;
There is a more subtle problem: minus is a function object in the
standard library. Since the code snippet has 'using namespace std',
there is a potential ambiguity here. The correct thing to do is to
remove 'using namespace std' and put
std::cout << n;
instead of
cout << n;
at the end of main().
As an aside, it's odd that many excellent C++ books omit std:: (often
not explicitly putting 'using namespace', perhaps only mentioning it
elsewhere). This is misleading when it is (IMHO) good practice to use
std:: explicitly, and the clarity gained by omitting it is marginal.
James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sat May 06, 2006 3:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | I need some help. I've been learning C++ using the tutorial at
Cplusplus.com (http://www.cplusplus.com/doc/tutorial/pointers.html is
the page in question).
|
Haven't looked at cplusplus.com for a while but when I did it was a horrible
mess of bad code, bad advise and incomplete 'references'.
| Quote: | int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
[...] |
| Quote: | p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
|
Okay, first thing to do in case of problems is to reduce this to the minimal
example to demonstrate the problem. As it turns out, the declaration
of 'subtraction' and 'minus' are enough already. The point is that the
declaration of 'minus' is simply bogus. If 'minus' is supposed to be a
function pointer, this would be the syntax:
int (*minus)(int,int) = subtraction;
which makes use of the implicit conversion from a function to a funtion
pointer. Making that explicit it would be
int (*minus)(int,int) = &subtraction;
The example would then compile, AFAICT (didn't try it).
However, this again strengthens my doubts about the quality of that website.
I rather think you should go to accu.org and pick a good book from the
reviews section.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
<somelawsbelongtoyou (AT) gmail (DOT) com> wrote in message
news:1146793338.818398.308210 (AT) g10g2000cwb (DOT) googlegroups.com...
[...]
| Quote: | #include <iostream
using namespace std;
|
opening widely std namespace is usually a bad practice. In this case it can
bring std::minus into scope and cause ambiguity.
| Quote: |
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
|
In C++ you cannot assign to a function, but you can assign a function pointer:
int (*minus) (int,int) = subtraction;
| Quote: |
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
|
Now because you opened std namespace you need to disambiguate the call to minus:
n = operation (20, m, ::minus);
On a side note; make a habit of initializing variables at the point of
declaration, that will prevent common errors and your code may run faster. E.g.
int g = (*functocall)(x,y);
int m = operation (7, 5, addition);
--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
pan urian Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | Hello all,
#include <iostream
using namespace std;
|
You should avoid writing "using namespace std" in global scope. For
example, in this code, your "minus" gets ambigous because of struct
minus in std namespace. Here you need only std::cout, so using all the
namespace std does not make any good. Better use just std::cout.
| Quote: | int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
|
It should be int (*minus)(int, int) = substraction
| Quote: | p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
|
Because you forgot about the asterisk :-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
yeah, you can't set a function like you can a variable. you'd need to
set it as a pointer -- just like how you passed it in as an argument.
so,
int minus (int,int) = subtraction;
becomes
int (*minus)(int,int) = &subtraction;
When you call operation(), you need to pass function POINTERS to it. A
function name by itself isn't enough. so,
m = operation (7, 5, &addition);
n = operation (20, m, minus);
notice i didn't grab the address of minus -- it's already a function
pointer.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
n2xssvv g02gfr12930 Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | Hello all,
I need some help. I've been learning C++ using the tutorial at
Cplusplus.com (http://www.cplusplus.com/doc/tutorial/pointers.html is
the page in question). Near the bottom of the section on pointers,
there is a code sample which is supposed to be demonstrating using
pointers to point to functions. It didn't seem to make sense, so I
tried compiling it; it wouldn't compile! Since the author of the
tutorial didn't leave any contact information, I'm turning to you
folks. Can anyone correct the following code for me?
#include <iostream
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
// ERROR
int minus (int,int) = subtraction;
int (*minus)(int,int) = &subtraction;
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
// ERROR
m = operation (7, 5, addition);
m = operation (7, 5, &addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
Thanks for your help,
Danny
p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
|
With the highlighted changes it should work.
JB
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
On 5 May 2006 04:54:04 -0400, somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | int minus (int,int) = subtraction;
|
You forgot something here ... it should look like this:
int (*minus) (int,int) = subtraction;
--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
int operation (int x, int y, int (*functocall)(int,int))
|
Do you notice an important difference between the declaration of
the third parameter of the function operation and the declaration
of the variable minus? (it is supposed to be a variable -- a
pointer to function, which you initialize with the address-of
the function subtraction).
When you put int minus (int,int), you're declaring (prototyping)
a function, and not a variable that is a pointer-to-function.
In other words, make that int (*minus).
Now, there's another problem -- quite subtle, mind you!
There is already something called "minus" in the standard
library -- you're only including <iostream>, which means that
in principle you would not expect the minus (part of the
<functional> facility, if I'm not mistaken) to be included;
but then, every part of the Standard Library has all its
right to include any other parts of the library that it
decides to use to implement its functionality.
Apparently, your compiler's <iostream> is deciding to make
use of the STL function objects, and it is importing minus
in the namespace std, causing ambiguity. So, make it Minus
instead of minus. (no, do not listen to those who are going
to jump at it telling you that you must avoid at all costs
the use of "using namespace std;" )
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
BigBrian Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | Hello all,
I need some help. I've been learning C++ using the tutorial at
Cplusplus.com (http://www.cplusplus.com/doc/tutorial/pointers.html is
the page in question). Near the bottom of the section on pointers,
there is a code sample which is supposed to be demonstrating using
pointers to point to functions. It didn't seem to make sense, so I
tried compiling it; it wouldn't compile! Since the author of the
tutorial didn't leave any contact information, I'm turning to you
folks. Can anyone correct the following code for me?
#include <iostream
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
Thanks for your help,
Danny
p.s. The compile errors state that "function `int minus(int, int)' is
initialized like a variable" and "`minus' undeclared (first use this
function)".
|
What about something like this....
#include <iostream>
//yuck! using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
//int minus (int,int) = subtraction;
typedef int(op)(int,int);
op * minus = subtraction;
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
std::cout <<n;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Schüle Daniel Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com schrieb:
| Quote: | Hello all,
I need some help. I've been learning C++ using the tutorial at
Cplusplus.com (http://www.cplusplus.com/doc/tutorial/pointers.html is
the page in question). Near the bottom of the section on pointers,
there is a code sample which is supposed to be demonstrating using
pointers to point to functions. It didn't seem to make sense, so I
tried compiling it; it wouldn't compile! Since the author of the
tutorial didn't leave any contact information, I'm turning to you
folks. Can anyone correct the following code for me?
#include <iostream
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int minus (int,int) = subtraction;
|
int (*minus)(int,int) = substraction;
or
int (*minus)(int,int) = &substraction;
| Quote: |
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
|
return is not a function, no () required
it's like writing
int i = 1;
i; // make nothing
i = 2;; // second ; useless
| Quote: | }
int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
|
hth, Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
johnchx2@yahoo.com Guest
|
Posted: Sat May 06, 2006 4:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | Can anyone correct the following code for me?
|
There are two errors here.
(snip)
| Quote: |
int minus (int,int) = subtraction;
|
You copied this line incorrectly from the tutorial. Copying it
correctly will fix one of the error messages you receive.
The other mistake is in the tutorial code itself:
using namespace std;
This is a wonderful example of why you should never do this.
(Interestingly, the using directive saves typing std:: exactly once in
this example...so in addition to making the code fail to compile, it
also requires *extra* typing.)
Evidently, the header <iostream> includes, directly or indirectly, a
declaration of std::minus, an arithmetic function object template that
lives in the header <functional>. The Comeau compiler gives a more
helpful error message than g++:
"ComeauTest.c", line 23: error: "minus" is ambiguous
n = operation (20, m, minus);
This seems a lot clearer (as well as true) than the "minus undeclared'
error g++ reports.
The lesson here is that when you pull all of the names from the std
namespace into the global namespace, you're likely to run across
surprising conflicts and incompatibilities. That's why we *invented*
namespaces in the first place. :-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ben Cottrell Guest
|
Posted: Sat May 06, 2006 5:21 pm Post subject: Re: pointers to functions |
|
|
somelawsbelongtoyou (AT) gmail (DOT) com wrote:
| Quote: | int minus (int,int) = subtraction;
|
this should be:
int (*minus) (int,int) = subtraction;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|