如何在标识符“和其他人之前”追踪“语法错误:缺失”)的原因?

时间:2011-12-07 19:28:14

标签: c++ visual-studio syntax

我正在使用包含这些文件的visual studio在C中编写项目:

  1. multiThreadServer.cpp
  2. myLib.cpp
  3. myLib.h
  4. 1st(multiThreadServer.cpp)包含这些

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

    2nd(myLib.cpp)这些

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

    3nd(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'
    1\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 : ')'
    

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

    问题可能是什么?

1 个答案:

答案 0 :(得分:0)

除了建议here之外,您还可以解决头文件中的问题,而无需将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);
...