pointer to function. why doesn't this compiler

2

2boxers

Can somebody tell me why the following program fails to compile?
I am using gcc-3.4.0.

#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;
}

here is what i get when i try to compile:
g++ pointer4.cpp -o pointer4

pointer4.cpp: In function `int main()':
pointer4.cpp:23: error: `minus' undeclared (first use this function)
pointer4.cpp:23: error: (Each undeclared identifier is reported only
once for each function it appears in.)

It would seem to me that minus is being declared in line 10. i.e.
minus is being declared as a pointer to a function that accepts 2 int
arguments, returns type int, and is being assigned the address of
subtraction.

Thanks in advance for any suggestions,
ChasW
 
R

Ron Natalie

2boxers said:
Can somebody tell me why the following program fails to compile?
I am using gcc-3.4.0.

#include <iostream>
using namespace std;

Try changing the above line to
using std::cout;
 
J

Juergen Heinzl

Can somebody tell me why the following program fails to compile?
I am using gcc-3.4.0.

#include <iostream>
using namespace std;
[-]
Bad idea, because ...
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;
}

here is what i get when i try to compile:
g++ pointer4.cpp -o pointer4

pointer4.cpp: In function `int main()':
pointer4.cpp:23: error: `minus' undeclared (first use this function)
pointer4.cpp:23: error: (Each undeclared identifier is reported only
once for each function it appears in.)
[-]
.... minus is also a STL function object.
Thanks in advance for any suggestions,
[-]
Either don't use using namespace std a./o. put your stuff into a separate
namespace (you know what namespaces are good for now ;-) ) or use a
different naming scheme like minus_( ... ) of minus_fp( ... ).

The namespace solution is the most flexible, though,
Juergen
 
2

2boxers

Try changing the above line to
using std::cout;

Since posting I realized that minus was the name of a template
function in one of the compiler's header files.

gcc-3.2.2 was kind enough to give an error that called my use of
"minus" ambiguous and then pointed out that minus was a function being
used elsewhere as well.

gcc-3.4.0 erroneuosly says that the "minus" is undeclared which is
just wrong.

using std::cout; works as does changing the name minus to Minus as I
realized just before my post.

Thanks,
ChasW
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top