LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-15-2006, 10:39 AM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
C++ - problem with pointers to class member functions


There's an exercise in "Sams Teach Yourself C++ for Linux in 21 Days" that I've written code for, but can't get to compile.

In this exercise, you have to write a class with one member variable and one private static variable and then write a pointer to access the non static member data and print the value.

My code (and yes, I know the variable names are bad!):
Code:
#include <iostream>
using namespace std;

class Test
{
public:
  Test(int a):itsA(a) { counter++; }
  virtual ~Test() { counter--; }
  int getA() { return itsA; }
  static int getHowMany() { return counter; }
private:
  int itsA;
  static int counter;
};

int Test::counter = 0;

int main()
{
  const int max = 3;
  int (Test::*pFunc)();
  pFunc = Test::getA;

  Test *TestArray[max];

  for(int i = 0; i < max; i++)
  {
    TestArray[i] = new Test(2*i);
    cout << "Test" << i << " = " << (TestArray[i]->*pFunc)() << endl;
  }
  
  return 0;
}
The book's examples are written in this way and I did have problems compiling some of them.

The error I'm getting with my own code is:

ex14-1.cc: In function `int main()':
ex14-1.cc:22: error: assuming pointer to member `int Test::getA()'
ex14-1.cc:22: error: (a pointer to member can only be formed with `&Test::getA
')

Edit: I have made the offending line bold, so it's easy to spot.

I think this has to do with my version of GCC/g++ (3.3.4) and this book refers to old versions (2.7.2, but they say 2.9.5 gives warnings). However, I'm not sure how to re-write my code to get it to work.

Thanks!

Last edited by Nylex; 01-15-2006 at 10:41 AM.
 
Old 01-15-2006, 11:09 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Change your line to:

Code:
  pFunc = &Test::getA;
graeme.
 
Old 01-15-2006, 11:11 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
By the way this is an excellent resource.

for pointer member-functions

graeme.
 
Old 01-15-2006, 11:14 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Thanks graemef! Lol, I didn't realise that error message was saying literally that, oops. Thanks for the link as well!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C Programming: Help with Pointers to Functions devinWhalen Programming 2 03-04-2005 02:00 PM
Need help with C++ class functions :( starla827 Programming 4 03-26-2004 08:43 PM
question on pointers to functions h/w Programming 3 10-06-2003 05:51 PM
pointers to functions/member functions champ Programming 2 03-28-2003 07:22 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 11:47 AM


All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration