0
votes

I'm getting "Only static const integral data members can be initialized within a class" in my code. I've tried searching up this error, but haven't been able to apply it to my code. This is my code:

#ifndef PROCESS_H
#define PROCESS_H

#pragma once

#include "Module.h"

#define SAFE_DELETE_VECTOR(vec) if (!vec.empty()) { \
                                    for (auto& slot : vec) { \
                                        delete slot; \
                                        slot = nullptr; \
                                    } \
                                }

class Process {
public:
                            Process();
                            ~Process();

    bool                    Attach(const std::string& ExeName);
    void                    Detach();

    bool                    Read(DWORD_PTR dwAddress, LPVOID lpBuffer, DWORD_PTR dwSize);
    bool                    Write(DWORD_PTR dwAddress, LPCVOID lpBuffer, DWORD_PTR dwSize);

    Module*                 GetModule(const std::string& ImageName);
    HMODULE                 LoadRemote(const std::string& ImageName);

    DWORD_PTR               Scan(DWORD_PTR dwStart, DWORD_PTR dwSize, const char* pSignature, const char* pMask);

    template<typename T>
    T Read(DWORD_PTR dwAddress, const T& tDefault = T()) {
        T tRet;

        if (!Read(dwAddress, &tRet, sizeof(T))) {
            return tDefault;
        }

        return tRet;
    }

    template<typename T>
    bool Write(DWORD_PTR dwAddress, const T& tValue) {
        return Write(dwAddress, &tValue, sizeof(T));
    }

private:
    bool                    DumpModList();
    DWORD                   GetProcessIdByName(const std::string& name);

private:
    HANDLE                  m_hProcess = NULL;
    DWORD                   m_dwProcessId = NULL;
    std::vector<Module*>    m_pModList;
};

extern Process* process;
extern Module*  client;
extern Module*  engine;

#endif // PROCESS_H

It's happening here:

HANDLE                  m_hProcess = NULL;

I've tried adjusting multiple things but failed. So, can anybody help out?

2
The data members ´m_hProcess´ and ´m_dwProcessId´ are not static and should be initialized in the class constructor and not where they are declared.Julian
@Julian Not any more. The issue is that the compiler doesn't support current C++.juanchopanza
@juanchopanza Thanks for the hint:-) Yet another C++11 feature I didn't know about...Julian

2 Answers

1
votes

Remove the = NULL from the declaration, and initialize the member in the constructor initializer list:

Process::Process(): m_hProcess(NULL) { /* ... */ }

Or upgrade to a compiler with C++11 support.

0
votes

Side nitpick: The SAFE_DELETE_VECTOR macro is horrible and has no place in C++ code. It won't help you with speeding anything up.

You should have an anonymous namespace function instead, without the redundant empty check:

namespace {
template <typename C> void safeDelete(C & container) {
  for (auto & slot : container) {
    delete slot;
    slot = nullptr;
  }
}
}