0

When I compile the code below I get the following error. help me...!

Errors:

Error C2276 '&': illegal operation on bound member function expression
Error C3867 'CCore::Run': non - standard syntax; use '&' to create a pointer to member

I'm not sure if the program gives me an error.
I want to run the "Run" function of the Core class.
Core.cpp file contains only the functions created by the compiler.

I'm learning English so I'm not good yet. So please understand that the whole code.

// main.cpp
#include "Core.h"
#include <thread>

int main()
{
    // The conditions below have been success
    if (CCore::GetInstance().Init())
    {
        // The code below fails to compile.
        // Error C3867 'CCore::Run': non - standard syntax; use '&' to create a pointer to member
        thread main_thread(CCore::GetInstance().Run);

        // Error C2276 '&': illegal operation on bound member function expression
        thread main_thread(&CCore::GetInstance().Run);

        main_thread.join();
    }
    return 0;
}
// Core.h
#pragma once
#include "Singleton.h"
#include <iostream>
using namespace std;

class CCore : public Singleton<CCore>
{
public:
    CCore();
    ~CCore();
    bool Init();
    void Run();
};
// Singleton.h
#pragma once

template<typename T>
class Singleton
{
protected:
    Singleton()=default;
    ~Singleton()=default;
public:
    static T& GetInstance()
    {
        static T instance;
        return instance;
    }
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton) = delete;
};
JeJo
  • 30,635
  • 6
  • 49
  • 88

1 Answers1

0

The compiler tells you the problem: Run() isn't a free function. It's a method, i.e. a function bound to an instance of an object. You have several options, but generally you either let the compiler synthesize a runnable for you, or write a free function yourself:

  1. Let the compiler do all the work: guess what, it can make singletons for you so how cool is that?!

    std::thread main_thread([]{
      static CCore myCore;
      myCore.Run();
    });
    
  2. You want to access that core? Sure!

    std::future<CCore*> coreWhenDone = std::async([]{
      static CCore myCore;
      myCore.Run();
      return &myCore;
    });
    

    Better yet, the core would provide some result, so that instead of accessing it directly when it's done, you could get its result (e.g. an int or std::vector<double> or whatever that core is computing)/

  3. Let the compiler do some of the work:

    std::thread main_thread([]{ CCore::GetInstance().Run(); });
    
  4. Split the work between yourself and the compiler:

    std::thread main_thread(std::bind(&CCore::Run, &CCore::GetInstance()));
    
  5. Do all the work yourself:

    void runCoreRun() {
      CCore::GetInstance().Run();
    }
    
    ...
    std::thread main_thread(&runCoreRun);
    
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • May I ask a question? If you have a blog that you usually refer to, please share it. And thank you for solving the above problem. – LeeSangMin Nov 01 '19 at 13:04
  • I don't refer much to resources outside my own head for basic answers. Qt documentation and https://cppreference.com are the main resources I use to look up API when an IDE with C++ typeahead is not available and my head offers no help :) I keep code from most of my SO answers on github: https://github.com/KubaO/stackoverflown – Kuba hasn't forgotten Monica Nov 06 '19 at 12:31