如何追查"syntax error: missing ')' before identifier"等原因?

How to track down the cause of "syntax error: missing ')' before identifier" and others?

本文关键字:identifier before 何追查 error missing syntax      更新时间:2023-10-16

我正在用visual studio用C编写一个项目,其中包含以下文件:

  1. multiThreadServer.cpp
  2. myLib.cpp
  3. myLib.h

第一个(multiThreadServer.cpp)包括这些

#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include "myLib.h"

second (myLib.cpp) these

#include <WinSock2.h>
#include <stdio.h>
#include "myLib.h"

第3页(myLib.h)不包含任何内容

在。h文件中我定义了这些函数:

// Starts up the server.
INT start_server(const unsigned short port);
// Accept Connections.
BOOL accept_connections();
// Accept Client.
BOOL AcceptClient(PCLIENT current_client);
// Receiver Function for the thread.
DWORD WINAPI Receiver(LPVOID lpParam);
// Receive data from client.
BOOL recv_data(PCLIENT current_client, char *buffer, int size);
// End server.
VOID end_server();
// Send data.
BOOL send_data(PCLIENT current_client, char *buffer, int size);
// Disconnect Client.
VOID disconnect_client(PCLIENT current_client);
// Send Data to all clients.
BOOL send_data_to_all(char *message);

这是myLib.cpp的一部分:

typedef struct _client{
    SOCKADDR_IN     address;        // internal data structure regarding this client
    SOCKET          socket;         // this clients socket
    BOOL            connected;      // is this client connected
    char            IP[20];         // this clients IP address
    int             address_length; // internal data structure regarding this client
} CLIENT, *PCLIENT;

现在,当我要编译整个项目时,这些恼人的语法错误返回:

1>  myLib.cpp
mylib.h(8): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(8): error C2061: syntax error : identifier 'current_client'
mylib.h(8): error C2059: syntax error : ';'
mylib.h(8): error C2059: syntax error : ')'
mylib.h(14): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(14): error C2061: syntax error : identifier 'current_client'
mylib.h(14): error C2059: syntax error : ';'
mylib.h(14): error C2059: syntax error : ','
mylib.h(14): error C2059: syntax error : ')'
mylib.h(20): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(20): error C2061: syntax error : identifier 'current_client'
mylib.h(20): error C2059: syntax error : ';'
mylib.h(20): error C2059: syntax error : ','
mylib.h(20): error C2059: syntax error : ')'
mylib.h(23): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(23): error C2061: syntax error : identifier 'current_client'
mylib.h(23): error C2059: syntax error : ';'
mylib.h(23): error C2059: syntax error : ')'
mylib.cpp(103): warning C4013: 'AcceptClient' undefined; assuming extern returning int
mylib.cpp(168): warning C4013: 'recv_data' undefined; assuming extern returning int
mylib.cpp(188): warning C4013: 'send_data' undefined; assuming extern returning int
mylib.cpp(189): warning C4013: 'disconnect_client' undefined; assuming extern returning int
mylib.cpp(270): error C2371: 'disconnect_client' : redefinition; different basic types
1>  multiThreadServer.cpp
mylib.h(8): error C2146: syntax error : missing ')' before identifier 'current_client'
1mylib.h(8): error C2061: syntax error : identifier 'current_client'
mylib.h(8): error C2059: syntax error : ';'
mylib.h(8): error C2059: syntax error : ')'
mylib.h(14): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(14): error C2061: syntax error : identifier 'current_client'
mylib.h(14): error C2059: syntax error : ';'
mylib.h(14): error C2059: syntax error : ','
mylib.h(14): error C2059: syntax error : ')'
mylib.h(20): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(20): error C2061: syntax error : identifier 'current_client'
mylib.h(20): error C2059: syntax error : ';'
mylib.h(20): error C2059: syntax error : ','
mylib.h(20): error C2059: syntax error : ')'
mylib.h(23): error C2146: syntax error : missing ')' before identifier 'current_client'
mylib.h(23): error C2061: syntax error : identifier 'current_client'
mylib.h(23): error C2059: syntax error : ';'
mylib.h(23): error C2059: syntax error : ')'

我现在在网上搜索1.30小时,但我找不到解决它的方法。

与这里建议的不同,您可以在头文件中修复问题,而无需实际将PCLIENT的定义移动到头文件中:

...
struct _client;
...
// Accept Client.
BOOL AcceptClient(struct _client* current_client);
...
// Receive data from client.
BOOL recv_data(struct _client* current_client, char *buffer, int size);
...
// Send data.
BOOL send_data(struct _client* current_client, char *buffer, int size);
// Disconnect Client.
VOID disconnect_client(struct _client* current_client);
...