use C++98. I have a struct t_fd
which is used inside a class MS
. In the struct there are two pointers to function: fct_read, fct_write
. I designed that the function pointers are pointing to the two methods of the class. But then I have this error when trying to call them.
expression preceding parentheses of apparent call must have (pointer-to-) function type.
Please advice on the error, also on the design. I need the two functions are methods of that class because I need to use class's attributes (even though it isn't showed here for the shake of simplicity). Thank you for your time, I appreciate your help!
#include <vector>
#include <iostream>
typedef struct s_fd {
void(MS::*fct_read) (int);
void(MS::*fct_write) (int);
} t_fd;
class MS
{
private:
std::vector< t_fd > _fdSet;
void server_accept(int s)
{
if (s % 2 == 0)
_fdSet[cs].fct_read = MS::client_read;
else
_fdSet[cs].fct_write = MS::client_write;
}
void client_read(int fd)
{
std::cout << "I'm reading\n";
}
void client_write(int fd)
{
std::cout << "I'm writing\n";
}
void check_fd()
{
int i = 0;
int size = 10;
while (i < size)
{
if (i < 5)
_fdSet[i].fct_read(i); //Error here!
if (i >= 5)
_fdSet[i].fct_write(i); //Error here!
i++;
}
}
};
The intent of your code is difficult to understand (in its current form). But I would be happy to solve few issues in your code.
class MS; // forward declaration
typedef struct s_fd {
void(MS::* fct_read) (int);
void(MS::* fct_write) (int);
} t_fd;
class MS
{ ... }
_fdSet[cs].fct_read = &MS::client_read;
if (i < 5) {
auto fptr = _fdSet[i].fct_read;
(this->*fptr)(i);
}