【发布时间】:2013-06-07 19:04:27
【问题描述】:
我试图在 struct 中分配一个函数,到目前为止我有这个代码:
typedef struct client_t client_t, *pno;
struct client_t
{
pid_t pid;
char password[TAM_MAX]; // -> 50 chars
pno next;
pno AddClient()
{
/* code */
}
};
int main()
{
client_t client;
// code ..
client.AddClient();
}
**Error**: *client.h:24:2: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token.*
正确的做法是什么?
【问题讨论】:
-
C 的结构中不能有函数;你可以尝试通过函数指针来粗略地模拟它。
-
函数指针是可接受的替代品吗? stackoverflow.com/a/840703/635678