Function Pointer Help

Hi,

I'm trying to reference a function using a function pointer. Any help is appreciated. The problem might be simple but I'm not seeing it.


http://pastebin.com/e4uhffWr

Vivienne

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//// In Header
struct PanelTab
{
    const char *tabName;                // should be the tab name
    void (*handler)(void);                  /// should be the function
};
 
//In Cpp
PanelTab RightFrameImGuiUI::tabNames[] = {{"Inspector",RightFrameImGuiUI::ShowSettingsPanel},
                                         {"ViewSettings", RightFrameImGuiUI::ShowSettingsPanel}};
 
void RightFrameImGuiUI::MainRandomFunction
{
 
    void (*DoPanel)() = tabNames[0].handler;
}

Last edited on
Is RightFrameIMGuiUI a namespace? A class? You haven't given us much to go on. What's the problem? Is there an error message generated?

When presenting a code sample, please provide a complete, compilable snippet that reproduces whatever problem you're experiencing.
This is the full source. What I'm trying to do is be able to initialize a static structure array with panel names, and matching function that can be called. In MainRandomFunction for example or ShowRightFrame at the end is how I'm attempting to assign it but I'm getting.


error: request for member ‘handler’ in ‘tabNames[0]’, which is of non-class type ‘const char*’

when compiling.

http://pastebin.com/xuAT612x


1
2
3
warning: ‘typedef’ was ignored in this declaration [enabled by default]|
warning: ‘typedef’ was ignored in this declaration [enabled by default]|
error: request for member ‘handler’ in ‘tabNames[0]’, which is of non-class type ‘const char*’|

|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 

HEADER

#pragma once


// struct for panels
typedef struct PanelTab
{
    const char *tabName;
    void (*handler)(void);

};

class RightFrameImGuiUI : public Object
{
    URHO3D_OBJECT(RightFrameImGuiUI, Object);

public:
    RightFrameImGuiUI(Context * context);
    virtual ~RightFrameImGuiUI();

    // Show Right Frame
    static bool * ShowRightFrame(void);


protected:
private:
    static bool initialized;
    static PanelTab tabNames[];

    static void ShowSettingsPanel(void);
};


SOURCE

///////////////////////////////////////////////////////


bool RightFrameImGuiUI::initialized= false;

PanelTab RightFrameImGuiUI::tabNames[] = {{"Inspector",RightFrameImGuiUI::ShowSettingsPanel},
                                         {"ViewSettings", RightFrameImGuiUI::ShowSettingsPanel}};

RightFrameImGuiUI::RightFrameImGuiUI(Context * context = g_pApp->GetGameLogic()->GetContext())
    :Object(context)
{
    //ctor
}

bool * RightFrameImGuiUI::ShowRightFrame(void)
{
    // return p_opened;
    bool * p_opened = NULL;

    // create tool bar
    ImGuiWindowFlags window_flags = 0;

    // get windows size
    unsigned int Width = g_pApp->GetGraphics()->GetWidth();
    unsigned int Height = g_pApp->GetGraphics()->GetHeight();

    // set flags
    window_flags |= ImGuiWindowFlags_NoTitleBar;

    // create    window
    ImGui::Begin("RightFrame",  p_opened, ImVec2(200,Height-8-128), 0.5, window_flags  );


    ImGui::End();

    initialized = true;

    // test
    void (*DoPanel)() = (*tabNames[0].handler)();



    return p_opened;
}

void RightFrameImGuiUI::ShowSettingsPanel(void)
{
}
Last edited on
I tried changing the struct to

1
2
3
4
5
6
7
8
9
10
// struct for panels
typedef struct PanelTab
{
    const char *tabName;
    void (handler)(void);

};




and

1
2
PanelTab RightFrameImGuiUI::tabNames[] = {{"Inspector", &RightFrameImGuiUI::ShowSettingsPanel},
                                         {"ViewSettings", &RightFrameImGuiUI::ShowSettingsPanel}};



I'm getting too many initializers error.
That seems like a very misleading error message, which makes me think perhaps something else is going on there, but your initialization of DoPanel isn't correct.

Should be:
void(*DoPanel)() = tabNames[0].handler;
Last edited on
Topic archived. No new replies allowed.