1

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

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • 1
    What does "when I call getter function" mean? The shown one line of code doesn't carry any useful information. Where does it appear? Some function? Some method? Can you follow Stackoverflow's instructions for creating a [mre] that everyone can cut/paste, ***exactly as shown*** into an empty file, then try to compile and reproduce your compilation error? – Sam Varshavchik Sep 12 '21 at 23:53
  • @SamVarshavchik I edit my question – Parisa.H.R Sep 13 '21 at 00:16

1 Answers1

1

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

  • what should I do if I don't know status and want to get it from status function? or something like that my problem is that I don't know it is available or not I want to get it and sent it to `std::async` – Parisa.H.R Sep 13 '21 at 00:31
  • Your question is unclear. If you would like to dynamically allocate instances of your Test class, you can do so with the new operator, you can look [here](https://stackoverflow.com/questions/44678457/dynamic-objects-in-c/44678941) for more info. Additionally, if you make your Status hold booleans, you can evaluate them as either beign True/False. I'm not entirely sure what you're attempting to do here. For examples with using std::async you can look [here](https://www.cplusplus.com/reference/future/async/). – solarmoon12 Sep 13 '21 at 00:53
  • I want to set my status in another place and as it changed get it and send it to the thread. – Parisa.H.R Sep 13 '21 at 00:58
  • I believe for that kind of handling, you would have to a loop in your thread, consistently checking for a change in the value. – solarmoon12 Sep 13 '21 at 00:59