Request help with function pointers
Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd edn and am getting a problem. I have attached the examples I am using and the error messages (basically the same kind) which I got. I am unable to understand what is wrong when I have followed the book example perfectly (AFAIK) except for the variablenames which is immaterial.
Please explain what I did wrong. I am using:
$ g++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu Thread model: posix gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
Thank you.
Shriramana Sharma.
#include "stdio.h"
class myclass
{
public :
void f ( int ) ; void g ( int ) ; void h ( int ) ; void i ( int ) ; } ;
void myclass :: h ( int a ) { printf ( "%d", a ) ; }
int main ( void )
{
myclass myclassinstance ;
myclass * myclassinstancepointer = & myclassinstance ;
void ( myclass :: * pointertofunction ) ( int ) = & myclass :: h ;
// this means that pointertofunction is a pointer (*) to a function existing in the scope of myclass (myclass ::)
// and takes an int as input ( (int) ) and gives void as output ( void )
( myclassinstance . * pointertofunction ) ( 1 ) ;
// calls the function pointed to by pointertofunction and existing as a member of the class myclassinstance with 1 as the parameter
( myclassinstancepointer -> * pointertofunction ) ( 2 ) ;
// calls the function pointed to by pointertofunction and existing as a member of the class pointed to by myclassinstancepointer with 2 as the parameter
}
samjnaa@chandas:~/bn/learning$ g++ function-pointers-not-working.cpp -o fp
function-pointers-not-working.cpp: In function ���������int main()���������:
function-pointers-not-working.cpp:21: error: expected unqualified-id before ���������*��������� token
function-pointers-not-working.cpp:23: error: expected unqualified-id before ���������*��������� token#include "stdio.h"
class maths
{
private :
void square ( int a ) const { printf ( "The square of %d is %d\n", a, a * a ) ; }
void cube ( int a ) const { printf ( "The cube of %d is %d\n", a, a * a * a ) ; }
void fourthpower ( int a ) const { printf ( "The fourth power of %d is %d\n", a, a * a * a * a ) ; }
void ( maths :: * functionpointers [ 3 ] ) ( int ) const ;
/*
the above line declares an array called functionpointers that has three ( 3 ) elements which are all pointers ( * ) to functions in the scope of the class maths ( maths :: ) and take a single integer as parameter ( int ) and return nothing ( void ) and do not modify the contents of the class in anyway ( const )
*/
public :
maths ()
{
functionpointers [ 0 ] = & maths :: square ;
functionpointers [ 1 ] = & maths :: cube ;
functionpointers [ 2 ] = & maths :: fourthpower ;
}
void executefunction ( int functionnumber, int inputforfunction )
{
if ( functionnumber < 0 || functionnumber > 2 ) return ;
( this -> * functionpointers [ functionnumber ] ) ( inputforfunction ) ;
// this line calls the function which is pointed ( * ) to by the functionnumber-th element of the functionpointers array which is a member of the object pointed to by the current object pointer ( this ) and passes the function the value of the variable inputforfunction as a parameter
}
} ;int main ( void )
{
maths mathsinstance ;
for ( int count = 0; count < 3; count ++ )
mathsinstance . executefunction ( count, 2 ) ;
}
samjnaa@chandas:~/bn/learning$ g++ function-pointers-2.cpp -o fp2 -Wall
function-pointers-2.cpp: In member function ���������void maths::executefunction(int, int)���������:
function-pointers-2.cpp:23: error: expected unqualified-id before ���������*��������� tokenQuoted 21 lines · Shriramana Sharma
Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd edn and am getting a problem. I have attached the examples I am using and the error messages (basically the same kind) which I got. I am unable to understand what is wrong when I have followed the book example perfectly (AFAIK) except for the variablenames which is immaterial.
Please explain what I did wrong. I am using:
$ g++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu Thread model: posix gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
Quoted 3 lines
Thank you.
Shriramana Sharma.
Remove the space in ". *" and "-> *".
Quoted 1 line · Stephen Kratzer
Remove the space in ". *" and "-> *".
Thank you very much -- that worked, but is there any good reason why I should not be allowed to put a space there? When I can use:
:: *
in the same programs without an error, why can't I use
. *
or
-> *
(I like having my code neatly and generously spaced since I feel it looks cleaner.)
I get no problem if I space the . and -> in other cases like:
QWidget . show () ;
or
this -> show () ;
So if there is no good reason for not being allowed to put a space between . and * or -> and *, I can report a bug against gcc asking for this error to be removed for being meaningless.
Thank you.
Shriramana Sharma.
Quoted 7 lines · Shriramana Sharma
Quoted 1 line
Remove the space in ". *" and "-> *".
Thank you very much -- that worked, but is there any good reason why I should not be allowed to put a space there? When I can use:
:: *
Quoted 7 lines
in the same programs without an error, why can't I use
. *
or
-> *
Because ".*" and "->*" are tokens, and you can't have a space in the middle of an token, in the same way that you can't put a space in the middle of "++", "+=", ">=" etc.
".*" looks like a combination of "." and "*", and "->*" looks like a combination of "->" and "*", and in a sense they behave that way, but ".*" and "->*" are actually separate tokens, not a combination.
Quoted 10 lines
(I like having my code neatly and generously spaced since I feel it looks cleaner.)
I get no problem if I space the . and -> in other cases like:
QWidget . show () ;
or
this -> show () ;
Here, "." and "->" are tokens. Putting a space between the "-" and ">" in "->" would similarly result in an error, as would a space in the middle of "th is". You can put a space between "(" and ")", though.
Quoted 3 lines
So if there is no good reason for not being allowed to put a space between . and * or -> and *, I can report a bug against gcc asking for this error to be removed for being meaningless.
No, gcc is correct.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 21 lines · Shriramana Sharma
Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd edn and am getting a problem. I have attached the examples I am using and the error messages (basically the same kind) which I got. I am unable to understand what is wrong when I have followed the book example perfectly (AFAIK) except for the variablenames which is immaterial.
Please explain what I did wrong. I am using:
$ g++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu Thread model: posix gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
Quoted 3 lines
Thank you.
Shriramana Sharma.
Remove the space in ". *" and "-> *".