Reputation: 391
I have written a class Seat in cpp & I declared a function pointer it it. class implimentation is given below
Seat.cpp
#include "StdAfx.h"
#include "Seat.h"
int Seat::count = FL;
Seat::Seat(void)
{
seatId = Seat::count;
seatStatus = unoccupied;
Seat::count++;
}
Seat::~Seat(void)
{
}
void Seat::*checkSeatStatus(void)
{
}
Seat.h
#include <string>
using namespace std;
class Seat :
public Vehicle
{
int seatId;
int seatStatus;
static int count;
public:
Seat(void);
~Seat(void);
void (*checkSeatStatus)();
};
it showing error at function pointer declaration :
'checkSeatStatus' : illegal use of type 'void'
What is the reason behind ? , does it need any initialization of function pointer ?
Upvotes: 0
Views: 102
Reputation: 409196
This doesn't answer the question you ask, but will show how to use the function in a thread.
Using std::thread
it's actually very easy to start a thread using any member function. The important part is that it should be a normal member function, so declared like
class Seat
{
...
public:
...
void checkSeatStatus();
};
Then to create a thread using the function you first need an instance of the class:
Seat seat;
And then you simply create the thread
std::thread my_thread{&Seat::checkSeatStatus, seat};
Do whatever other processing you want to do in the main thread, and then join the thread you had created:
my_thread.join();
There are a couple of important things to know here: The instance of the Seat
class (the variable seat
above) must live longer than the thread. If it goes out of scope and is destructed before the end of the thread that will lead to undefined behavior.
You must also join
the tread before the thread-object (the variable my_thread
above) is destructed.
Upvotes: 0
Reputation: 339816
If checkSeatStatus
is intended to be a member function it should be:
void* Seat::checkSeatStatus(void) {
...
}
with function prototype within the class declaration of:
void* checkSeatStatus(void);
If it's intended to be a member variable holding a function pointer that you can set then, umm, don't do that... It's very probably the wrong design.
If (per the name) it's just supposed to return the current value of status
then it should be a function that returns an int
instead of a void *
:
int Seat::checkStatus(void) {
return status;
}
NB: I removed Seat
from the method name since it should be implicit from the fact that you called it on a Seat
object.
Upvotes: 2