在 std::async 中使用的函数中调用我的 Struct 时调用不带 object 参数的 non-static 成员函数

call to non-static member function without an object argument when call my Struct in function that use in std::async

我有这个代码:

#include <future>
#include <iostream>

struct Status
{
    std::string Available;
    std::string Connected;
    std::string DisConnected;
};

class Test
{
 public:
   Test();

   Status &status()
   {
       return _status;
   }
   void setStatus(const Status &newStatus)
   {
       _status = newStatus;
   }

   std::string show()
   {

       return Status::Available;
   }

private:
    Status _status ;
};



int main()
{

    Test tst;

    auto value= std::async([](std::string str) { return str;} ,tst.show());

    std::cout <<value.get();


    return 0;
}

当我编译它时,我得到了这个错误:

illegal reference to non-static member 'Status::Available'

我不知道该如何解决这个问题,但这是因为我在 std::async 中使用了该功能。我也不知道为什么会这样。

您需要将其更改为_status.Available。 您正在尝试引用 Status 结构,而不是该结构的实例。