Hi!
I am using Dev C++ 4.9.9.2
on Microsoft XP Home
Am learning C++ using a tutorial from www.Cplusplus.com
Have 2 questions:
Problem with code:
// pointer to function
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 (*functioncall)(int, int))
{ int g;
g = (functioncall)(x, y);
return (g);
}
int main()
{
int n, m;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout << n;
system ("PAUSE");
return 0;
}
Does not compile: indicates that there is an
error when minus is called in main(): saying that minus is undeclared. ???
2nd question:
am having a lot of trouble w/ pointers.
I have read quite a few different docs about them and somewhere I must have missed something fundamental. I don't get the concept, almost like "Pointers?? so?? what's the big deal??"
I know they're important but ...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The simple solution is to use a different name. You could also avoid moving all std:: symbols to :: by replacing:
using namespace std ;
to
using std::cout ;
or replacing the line:
cout << n;
with
std::cout << n;
or wrap your symbols in their own namespace to separate them from the std:: symbols.
Although the tutorial at cplusplus.com is nicely presented and has been recently updated with 'namespace awareness', it is not particularly comprehensive and as you have found portable. Try http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html perhaps.
The emphasis on function pointers in that tutorial is probably misplaced - most uses of function pointers have better alternatives in C++ (as opposed to C).
A pointer is simply a memory address. A simple variable contains data, a pointer 'points' to data. The difference is that a simple variable always refers to the same object, whereas a pointer can be moved to point to different objects at different times. Another use of pointers is to address objects and data that are instantiated dynamically. A further use is to exchange data between functions without having to copy possibly large memory objects.
You might think of a pointer as a variable that contains the address of data that exists elsewhere.
Again in C++ many uses of pointers are better solved in other ways. C++ has simple, pointer, and reference variables. Reference variables are useful for the efficient exchange of data between functions and can be safer than using pointers for this purpose.
Pointers is one of those things that most C/C++ novices have trouble with. You will probably just 'get it' at some point. Usually you start by using pointers only where interfaces you are using demand it, and eventually you find a problem that cannot easily or elagantly be solved without them; at that point the penny usually drops.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just to let you know, Clifford, I changed the name minus and that solved the problem. Thanks.
Thanks to you and the others who answered my question 2. I have checked all your suggestions and am well inspired.
Thanks again!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi!
I am using Dev C++ 4.9.9.2
on Microsoft XP Home
Am learning C++ using a tutorial from www.Cplusplus.com
Have 2 questions:
// pointer to function
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 (*functioncall)(int, int))
{ int g;
g = (functioncall)(x, y);
return (g);
}
int main()
{
int n, m;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout << n;
system ("PAUSE");
return 0;
}
Does not compile: indicates that there is an
error when minus is called in main(): saying that minus is undeclared. ???
2nd question:
am having a lot of trouble w/ pointers.
I have read quite a few different docs about them and somewhere I must have missed something fundamental. I don't get the concept, almost like "Pointers?? so?? what's the big deal??"
I know they're important but ...
It took a while to figure out, but this is simply a name clash with a symbol in the std:: namespace. 'minus' is a symbol used in the STL implementation (see http://www.cs.brown.edu/~jwicks/libstdc++/html_user/structstd_1_1minus.html).
The simple solution is to use a different name. You could also avoid moving all std:: symbols to :: by replacing:
using namespace std ;
to
using std::cout ;
or replacing the line:
cout << n;
with
std::cout << n;
or wrap your symbols in their own namespace to separate them from the std:: symbols.
Although the tutorial at cplusplus.com is nicely presented and has been recently updated with 'namespace awareness', it is not particularly comprehensive and as you have found portable. Try http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html perhaps.
The emphasis on function pointers in that tutorial is probably misplaced - most uses of function pointers have better alternatives in C++ (as opposed to C).
A pointer is simply a memory address. A simple variable contains data, a pointer 'points' to data. The difference is that a simple variable always refers to the same object, whereas a pointer can be moved to point to different objects at different times. Another use of pointers is to address objects and data that are instantiated dynamically. A further use is to exchange data between functions without having to copy possibly large memory objects.
You might think of a pointer as a variable that contains the address of data that exists elsewhere.
Again in C++ many uses of pointers are better solved in other ways. C++ has simple, pointer, and reference variables. Reference variables are useful for the efficient exchange of data between functions and can be safer than using pointers for this purpose.
Pointers is one of those things that most C/C++ novices have trouble with. You will probably just 'get it' at some point. Usually you start by using pointers only where interfaces you are using demand it, and eventually you find a problem that cannot easily or elagantly be solved without them; at that point the penny usually drops.
Clifford
Thank's Clifford.
At cprogramming.com is also a tutorial about pointers, and especially function pointers. But don't know if this is better.
I'll check it out.
Thanx pronecracker
www.google.com: c++ parashift FAQ
Just to let you know, Clifford, I changed the name minus and that solved the problem. Thanks.
Thanks to you and the others who answered my question 2. I have checked all your suggestions and am well inspired.
Thanks again!