简体   繁体   中英

C++ Structure default constructor

I'm using a part of autoit source code
I have a structure :

typedef struct
{
    WPARAM  wParam;                             // Hotkey ID
    LPARAM  lParam;                             // Key and modifiers (control, alt, etc)
    luabridge::LuaRef lFunction;                // Function to call
} HotKeyDetails;

I make an array of this structure in my class private storage :

static HotKeyDetails    *m_HotKeyDetails[MAXHOTKEYS];   // Array for tracking hotkey details

And when I wanna create a new of this structure :

m_HotKeyDetails[nFreeHandle] = new HotKeyDetails;   // Create new entry

I get this error :

1>Scripts.cpp(1216): error C2512: 'HotKeyDetails' : no appropriate default constructor available

How ever I just copy/pasted code from autoit source code but in there it will compile well
Whats wrong with that

It looks like the class luabridge::LuaRef has no default constructor (see the documentation ), so the compiler can't generate a default constructor for HotKeyDetails either.

To fix this, just add a constructor; for example:

struct HotKeyDetails {
    WPARAM  wParam;                             // Hotkey ID
    LPARAM  lParam;                             // Key and modifiers (control, alt, etc)
    luabridge::LuaRef lFunction;                // Function to call

    explicit HotKeyDetails(lua_State* L): wParam(NULL), lParam(NULL), lFunction(L) {}
};

If you prefer to allow this struct to be default constructible, then you'll have to make lFunction a pointer:

struct HotKeyDetails {
    WPARAM  wParam;                             // Hotkey ID
    LPARAM  lParam;                             // Key and modifiers (control, alt, etc)
    luabridge::LuaRef *lFunction;               // Function to call

    HotKeyDetails(): wParam(NULL), lParam(NULL), lFunction(NULL) {}
};

try to add a default constructor to the HotKeyDetails struct. Like:

struct HotKeyDetails
{
    HotKeyDetails() : wParam(0), lParam(0), lFunction(L) {}

    WPARAM  wParam;                             // Hotkey ID
    LPARAM  lParam;                             // Key and modifiers (control, alt, etc)
    luabridge::LuaRef lFunction;                // Function to call
} ;

However you will need to provide/track the lua_State* L variable which was provided by your application if you want to use this struct in an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM