By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

pointer to function. why doesn't this compiler

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"2boxers" <2boxers_at_comcast_dot_net> wrote in message news:vg********************************@4ax.com...
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;
Jul 22 '05 #2

P: n/a
In article <vg********************************@4ax.com>, 2boxers wrote:
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

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Jul 22 '05 #3

P: n/a
On Thu, 27 May 2004 17:35:56 -0400, "Ron Natalie" <ro*@sensor.com>
wrote:

"2boxers" <2boxers_at_comcast_dot_net> wrote in message news:vg********************************@4ax.com...
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;


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.