Pointer Issues
Author |
Message |
Clayton
|
Posted: Wed Nov 29, 2006 6:56 pm Post subject: Pointer Issues |
|
|
Hey guys, I was reading through some C++ tuts, and I was fine until I hit pointers, (well I understand so far, but this program has me stumped). This is a chunk of code that was on there, and I didn't understand what was going on, so I decided to cut, paste and compile to see what the output was, and it didn't even work! So now I'm trying to figure out why, but I'm not getting anywhere. Any help on where and why this isn't working would be great. The error I'm getting from my compiler is this:
pointtest.cpp: In function 'int main()':
pointtest.cpp:24: error: 'minus' undeclared (first use this function)
pointtest.cpp:24: error: (Each undeclared identifier is reported only once for each function it appears in.)
and heres the code :
c++: |
// pointer to functions
#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 in advance for the help.[/list] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
r.3volved
|
Posted: Wed Nov 29, 2006 7:24 pm Post subject: (No subject) |
|
|
thats a pointer named `minus` instantiated to point to the function `subtraction`
so it's not a function itself as your compiler thinks it is since you set it up as a function definition, but rather a pointer to another function |
|
|
|
|
|
Clayton
|
Posted: Wed Nov 29, 2006 7:29 pm Post subject: (No subject) |
|
|
yes, but do you not pass a pointer (pointing to a function) to another function so that you can use the function pointed to by the pointer within the function (dereferenced of course)? |
|
|
|
|
|
wtd
|
Posted: Wed Nov 29, 2006 8:30 pm Post subject: (No subject) |
|
|
And this is why C++ templates are beautiful things.
code: | #include <iostream>
#include <functional>
template <typename T, typename F>
T operation(T a, T b, F f)
{
return f(a, b);
}
int main()
{
std::cout << operation(3, 4, std::plus<int>()) << std::endl
<< operation(2, 1, std::minus<int>()) << std::endl;
} |
|
|
|
|
|
|
Clayton
|
Posted: Wed Nov 29, 2006 8:35 pm Post subject: (No subject) |
|
|
I can see how that works, but I kinda wanted to figure out why this isn't working with pointers at the moment. |
|
|
|
|
|
Monstrosity_
|
Posted: Wed Nov 29, 2006 8:44 pm Post subject: (No subject) |
|
|
Freakman wrote: I can see how that works, but I kinda wanted to figure out why this isn't working with pointers at the moment.
Forum won't allow tinyurl's.. so here...
link
mod edit: tinyurl isn't needed, just be sure to use the url code tags properly |
|
|
|
|
|
wtd
|
Posted: Wed Nov 29, 2006 8:49 pm Post subject: (No subject) |
|
|
This code works. Aside from cleaning it up a bit with a typdef, and merging declaration and initialization, see if you can change the semantic change to the program.
code: | #include <iostream>
using namespace std;
typedef int (*BinaryIntOp)(int, int);
int addition(int a, int b)
{
return a + b;
}
int subtraction(int a, int b)
{
return a - b;
}
int operation(int x, int y, BinaryIntOp f)
{
return f(x, y);
}
int main()
{
BinaryIntOp minus = subtraction;
int m = operation(7, 5, addition);
int n = operation(20, m, minus);
cout << n << endl;
}
|
|
|
|
|
|
|
Clayton
|
Posted: Wed Nov 29, 2006 9:09 pm Post subject: (No subject) |
|
|
okay, I was fine with just about everything except for
code: |
typedef int (*BinaryIntOp)(int, int);
|
what exactly is going on? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Nov 29, 2006 9:25 pm Post subject: (No subject) |
|
|
The type for a fucntion that takes two ints, and returns an int is:
I typefed that to BinaryIntOp. |
|
|
|
|
|
Clayton
|
Posted: Wed Nov 29, 2006 9:28 pm Post subject: (No subject) |
|
|
ahh, excellent, that makes more sense now |
|
|
|
|
|
r.3volved
|
Posted: Wed Nov 29, 2006 9:46 pm Post subject: (No subject) |
|
|
If you're just getting into pointers now, this might help you out a bit
Just a list of different pointer types and declarations
code: |
int* p1; // pointer to int
int const* p2; // pointer to const int
const int* p3; // pointer to const int
int* const p4 = 0; // constant pointer to an int
int a1[5]; // array of 5 integers
int* p5[5]; // array of 5 integer pointers
int* p6 = a1; // pointer to array of 5 integers (pointers == one dimentional arrays)
int const* p7[5]; // array of 5 pointers to constant integers
int const* * pp7 = p7; // Pointer to array of pointers
int f(int); // function signature: takes int, returns int
int* p8(int); // function signature: takes int, returns pointer to an integer
int* const p9( int const& );// function signature: takes reference to constant int, returns const ptr to int
int (*p10)(int); // pointer to function: takes int, return int
|
|
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Dec 02, 2006 2:46 pm Post subject: (No subject) |
|
|
This is an interesting topic. Some comments though.
wtd wrote: The type for a fucntion that takes two ints, and returns an int is:
I typefed that to BinaryIntOp.
As you probably know, the type for a function that takes two ints and returns an int is:
r.3volved wrote: code: | // ...
int* p6 = a1; // pointer to array of 5 integers (pointers == one dimentional arrays)
int const* p7[5]; // array of 5 pointers to constant integers
int const* * pp7 = p7; // Pointer to array of pointers
// ...
|
This is even more interesting:
code: | // ...
int (*p6)[5] = &a1; // pointer to array of 5 integers
int const *p7[5]; // array of 5 pointers to constant integers
int const **pp7 = p7; // pointer to pointer to constant integers
int const *(*pp7_)[5] = &p7; // pointer to array of 5 pointers to constant integers
//...
|
At least, that's what I think (haven't checked with compiler, etc.). |
|
|
|
|
|
Clayton
|
Posted: Sat Dec 02, 2006 3:43 pm Post subject: (No subject) |
|
|
actually on the pointer to the array one, he is correct (at least according to the tutorial I am using)
because a1 is an array of 5 ints
then he declares the pointer.
|
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Dec 02, 2006 3:59 pm Post subject: (No subject) |
|
|
Actually, he does
c++: |
int a1[5];
int *p6 = a1; // pointer-to-int initialized with pointer-to-int (after array-to-pointer conversion)
|
I would be interested in seeing what the tutorial you are using says. Thanks. |
|
|
|
|
|
Clayton
|
Posted: Sat Dec 02, 2006 5:32 pm Post subject: (No subject) |
|
|
actually I made a typo in my post, so.. ya, I know what you are saying, and you are correct, it is the same thing I was trying to say |
|
|
|
|
|
|
|