1

So im trying to make a thread via SFML but my code is just generating an error ive been trying to fix for hours.

This is the error i keep getting:

    d:\c++\engine\deps\sfml-2.1\include\sfml\system\thread.inl(39): error C2064: term does not evaluate to a function taking 0 arguments
1>          d:\c++\engine\deps\sfml-2.1\include\sfml\system\thread.inl(39) : while compiling class template member function 'void sf::priv::ThreadFunctor<T>::run(void)'
1>          with
1>          [
1>              T=Thread *
1>          ]
1>          d:\c++\engine\deps\sfml-2.1\include\sfml\system\thread.inl(70) : see reference to class template instantiation 'sf::priv::ThreadFunctor<T>' being compiled
1>          with
1>          [
1>              T=Thread *
1>          ]
1>          d:\c++\engine\src\engine\thread.cpp(20) : see reference to function template instantiation 'sf::Thread::Thread<Thread*>(F)' being compiled
1>          with
1>          [
1>              F=Thread *
1>          ]

And here is my thread code

#include "Thread.h"

Thread::Thread(void)
{
    threadRunning = false;
}

ThreadException::ThreadException(string err)
{
    this->err = err;
}

void Thread::Begin()
{
    if(threadRunning == true) {
        throw ThreadException("Thread already running");
    }

    threadRunning = true;
    sf::Thread thread = (&Thread::ThreadProc, this);
    thread.launch();
}

bool Thread::IsRunning()
{
    return threadRunning;
}

Thread.cpp

#pragma once

#include <SFML\System.hpp>
#include <iostream>
using namespace std;

class Thread
{
private:
    bool threadRunning;
public:
    void Begin();

    Thread();

    bool IsRunning();

    void ThreadProc();
};

class ThreadException : public exception
{
public:
    const char * what() const throw()
    {
        return "Thread Exception";
    }
    std::string err;
    ThreadException(string err);
    ~ThreadException() throw() {};
};

So what i need is the fix or an explanation to what is going on here i have tried literally everything i can think of to fix this

James Young
  • 313
  • 3
  • 10
  • 19

1 Answers1

0
  1. Your function binding is wrong.
  2. If bound correctly it does not require the "this" argument.
  3. You didn't provide an implementation of your ThreadProc function.

    void Thread::Begin()
    {
        if(threadRunning == true) 
            {
                throw ThreadException("Thread already running");
            }
    
        threadRunning = true;
    
        std::function<void(void)> f = std::bind(&Thread::ThreadProc, this);
    
        sf::Thread thread = (f); // Don't need to use = here either, just call the ctor directly
        thread.launch();
    }
    
    void Thread::ThreadProc()
    {
    
    }
    

See this question for more info:

Using generic std::function objects with member functions in one class

Edit: Also if you don't use the assignment operator and call the constructor directly then your current syntax will work.

So:

sf::Thread thread(&Thread::ThreadProc, this);

Not:

sf::Thread thread = (&Thread::ThreadProc, this);
Community
  • 1
  • 1
paulm
  • 5,629
  • 7
  • 47
  • 70