Home > Net >  call to non-static member function without an object argument when call my Struct in function that u
call to non-static member function without an object argument when call my Struct in function that u

Time:09-17

I have this code :

#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;
}

as I compile it I get this error :

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

I don't know how should I fix this but it happens because of I use that function in std::async. also I don't know why this happens.

enter image description here

CodePudding user response:

You need to change it to _status.Available. You're attempting to reference the Status struct, rather than your instance of that structure.

  • Related