All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shriramana Sharma <samjnaa@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Request help with function pointers
Date: Thu, 22 Feb 2007 09:57:53 +0530	[thread overview]
Message-ID: <45DD1BC9.9030902@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 948 bytes --]

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.

[-- Attachment #2: function-pointers-1.cpp --]
[-- Type: text/x-c++src, Size: 936 bytes --]

#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
}

[-- Attachment #3: function-pointers-1-error --]
[-- Type: text/plain, Size: 464 bytes --]

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

[-- Attachment #4: function-pointers-2.cpp --]
[-- Type: text/x-c++src, Size: 1500 bytes --]

#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 ) ;
}

[-- Attachment #5: function-pointers-2-error --]
[-- Type: text/plain, Size: 337 bytes --]

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 ���������*��������� token

             reply	other threads:[~2007-02-22  4:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-22  4:27 Shriramana Sharma [this message]
2007-02-27 14:48 ` Request help with function pointers Stephen Kratzer
2007-02-28 15:32   ` Shriramana Sharma
2007-03-07 16:49     ` Glynn Clements
2007-02-27 15:15 ` Stephen Kratzer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45DD1BC9.9030902@gmail.com \
    --to=samjnaa@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.