Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:49:25+00:00 2026-05-27T10:49:25+00:00

To store the functions of a class in an array the following link http://sourcemaking.com/design_patterns/state/cpp/1

  • 0

To store the functions of a class in an array the following link http://sourcemaking.com/design_patterns/state/cpp/1 contains the code like below (Machine is the class name).

void(Machine:: *ptrs[])() = 
  {
    Machine::off, Machine::on
  };

The example in that link does not compile with the g++ compiler throwing error as below

$ g++ state.cpp 
state.cpp: In function ‘int main()’:
state.cpp:89:18: error: invalid use of non-static member function ‘void Machine::off()’
state.cpp:89:32: error: invalid use of non-static member function ‘void Machine::on()’
state.cpp:97:15: error: expected unqualified-id before ‘*’ token

I am using g++ version 4.5.2

$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Can an array be defined like this, i am not able to find an array defination like this any where else. If the example is correct why does not it compile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:49:26+00:00Added an answer on May 27, 2026 at 10:49 am

    If you define a typedef for your member functions you will simplify your code greatly.

    class Machine
    {
    public:
        void on(){}
        void off(){}
    };
    
    
    int main()
    {
    
        typedef void (Machine::*MachineFunctionPtr)();
    
        MachineFunctionPtr temp[] = { &Machine::off , &Machine::on };
    
    
       //To invoke a function use this syntax
        Machine mymachine;
       ((mymachine).*(temp[1]))();
    

    The being said your error is due to missing “&” before the function name.

    If you don’t want to use typedef the correct way is something like

    void(Machine:: *ptrs[])() = 
    {
        &Machine::off, &Machine::on
    };
    Machine fsm;
    int num;
    while (1)
    {
        cout << "Enter 0/1: ";
        cin >> num;
        ((fsm).*(ptrs[num]))();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is from a tutorial ( http://net.tutsplus.com/php/creating-a-php5-framework-part-1/ ), not mine. I have
I have an array of constant data like following: enum Language {GERMAN=LANG_DE, ENGLISH=LANG_EN, ...};
i have following captcha code: <?php class CaptchaView extends View { private $fontsDir =
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
I have been using anonymous namespaces to store local data and functions and wanted
I'm trying to store member function pointers by templates like this: (This is a
Consider the following proxy class: class VertexProxy { public: VertexProxy(double* x, double* y, double*
I have the following HTML (a snippet): <td class=ms-vb-title height=100%> <table class=ms-unselectedtitle onmouseover=OnItem(this) ctxname=ctx1
I want to store an array into a xml file. The array will be
My problem is the following; I have a class that uses non-static native methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.