473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer to function. why doesn't this compiler

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
3 1387

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
9
by: Richard | last post by:
I'm still battling with this causing UDB: while(e-- s); if s points to the start of a string and e becomes less than s then e is not really pointing to defined char. Fine. But UDB? Yes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.