2013-09-05 2 views
1

Так что я пытаюсь сделать поток через SFML, но мой код просто генерирует ошибку, которую я пытаюсь исправить в течение нескольких часов.Темы в создании ошибок SFML

Это ошибка я получаю:

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>   ] 

А вот мой нить код

#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() {}; 
}; 

Так что мне нужно это исправить или объяснение тому, что происходит здесь, я буквально пробовал все, что я могу придумать, чтобы исправить это.

ответ

0
  1. Неправильная функция связи.
  2. Если верно связать, это не требует аргумента «this».
  3. Вы не представили реализацию своей функции ThreadProc.

    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() 
    { 
    
    } 
    

Смотрите этот вопрос для получения дополнительной информации:

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

Edit: Кроме того, если вы не используете оператор присваивания и вызова конструктора напрямую, то ваш текущий синтаксис будет работать.

Итак:

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

Не:

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