【问题标题】:Threads in SFML creating errorsSFML 中的线程创建错误
【发布时间】:2013-09-09 16:49:46
【问题描述】:

所以我试图通过 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;
}

线程.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() {};
};

所以我需要的是修复或解释这里发生的事情我已经尝试了我能想到的一切来解决这个问题

【问题讨论】:

    标签: c++ multithreading visual-c++ sfml


    【解决方案1】:
    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

    编辑:此外,如果您不使用赋值运算符并直接调用构造函数,那么您当前的语法将起作用。

    所以:

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

    不是:

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

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2020-09-08
      • 2016-03-25
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多