【问题标题】:Syntax Error missing before constant error in C program在 C 程序中的常量错误之前缺少语法错误
【发布时间】:2013-12-02 08:31:22
【问题描述】:
#include "MAIN.h"
#define port 444    //port number
//port = 18017;

typedef unsigned _int8 uint8;
typedef unsigned _int16 uint16;
typedef unsigned _int32 uint32;

uint32 measurements[18];


void XcpApp_IpTransmit(uint16, Xcp_StatePtr8, uint16);

void main(void)
{

#ifdef XCP_ENABLE
      /*initialise the XCP Ecu Softwares */

    Xcp_Initialize();

#endif
//initialize before start of the operating system

    while(1)
    {
    Timer1();   
    Timer2();
    Timer3();
    }
}

/ERROR IN THIS LINE/ void XcpApp_IpTransmit(uint16 port, Xcp_StatePtr8 pBytes, uint16 numBytes) // 此函数必须在由端口标识的 IP 连接上传输指定字节 {

        pBytes = &measurements;     // pBytes points to address of the memory
        numBytes = 8;               //number of bytes at pBytes.

         struct sockaddr_in server;  // creating a socket address structure: structure contains ip address and port number
         WSADATA wsa;
         SOCKET s;
         int len;
        //int bytes_recieved;
        //char send_data[1024],recv_data[1024];

        printf("Initializing Winsock\n");
        if(WSAStartup(MAKEWORD(2,2), &wsa)!=0)
        {
            printf("Failed Error Code: %d", WSAGetLastError());
            return 1;
        }
        printf("Initialised\n");


        //CREATING a SOCKET

        if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            printf("Could not Create Socket\n");
            return 0;
        }
        printf("Socket Created\n");

        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        server.sin_family = AF_INET;     
        server.sin_port = htons("port");   
        len = sizeof(server);

        //SENDING a data

        /* bzero(&(server_addr.sin_zero),8); 

        if (connect(sock, (struct sockaddr *)&server_addr,
                    sizeof(struct sockaddr)) == -1) 
        {
            perror("Connect");
            exit(1);
        }*/

        while(1)
        {

          //bytes_recieved=recv(s,recv_data,1024,0)

          numBytes=recvfrom(s, pBytes, 8, 0, (struct sockaddr*)&server, &len);
          pBytes[numBytes] = '\0';

          if (strcmp(pBytes , "q") == 0 || strcmp(pBytes , "Q") == 0)
          {
           close(s);
           break;
          }

         /* else
           printf("\nreceived data = %s " , pBytes);*/
         // XcpIp_RxCallback (chunkLen, pChunkData, port); 

          /* 
           printf("\nSEND (q or Q to quit) : ");
           gets(send_data);

          if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
           send(s,send_data,strlen(send_data), 0); */

          else
          {
              //after the data has ben transmitted
           XcpIp_TxCallback (port, numTxBytes);
           //send(s,send_data,strlen(send_data), 0); 
           closesocket(s);
           WSACleanup();
           break;
          }
        }


}

我创建了一个 18*4 字节的内存,指定了端口号,然后我正在调用一个函数 void XcpApp_IpTransmit(uint16, Xcp_StatePtr8, uint16); 将数据从内存传输到指定的端口号。我正在主要功能中执行一些任务。稍后为被调用函数编写函数定义(创建一个套接字并通过端口号和IP地址发送数据) 我收到这样的错误:

error C2143: syntax error : missing ')' before 'constant'
error C2143: syntax error : missing '{' before 'constant'
error C2059: syntax error : '<Unknown>'
error C2059: syntax error : ')'

谁能帮我解决这个错误(错误在函数体 void XcpApp_IpTransmit(uint16, Xcp_StatePtr8, uint16))???????

【问题讨论】:

  • 检查导致语法错误的最佳方法是注释部分程序并验证错误是否消失。这样做直到找到有问题的地方。你可以自己做。
  • 你能把代码贴在 MAIN.h 里吗?
  • 发布 MAIN.h,也许有帮助
  • #pragma once #pragma comment (lib, "ws2_32.lib") typedef unsigned _int16 uint16; typedef 无符号 _int32 uint32; typedef _int32 int32; typedef 无符号 _int8 uint8; typedef unsigned long uint32; #include #include #include #include "xcp.h" #include "LocatedVars.h" #include #include "Task2ms.h" #包括 "Task10ms.h" #include "Task100ms.h" #include "TIMER1.h" #include "TIMER2.h" #include "TIMER3.h" #include "xcp_pub.h" #include "xcpip_pub.h" #include "stopwatch.h" #include "xcp_common.h" #include "xcp_auto_conf.h"#include "xcp_auto_confpriv.h"
  • 请将相关信息添加到问题本身,但将其压缩到 cmets 中,很难阅读。谢谢。

标签: c windows sockets memory


【解决方案1】:

如果这是一个C 程序,我看到的第一个错误是你在函数XcpApp_IpTransmit() 中的变量声明不是它们作用域的第一行。

换句话说,尝试将该函数的前几行更改为以下内容:

void XcpApp_IpTransmit(uint16 port, Xcp_StatePtr8 pBytes, uint16 numBytes) // this function must transmit the specified byte on IP connection identified by port
{
     struct sockaddr_in server;  // creating a socket address structure: structure contains ip address and port number
     WSADATA wsa;
     SOCKET s;
     int len;
     //int bytes_recieved;
     //char send_data[1024],recv_data[1024];

     pBytes = &measurements;     // pBytes points to address of the memory
     numBytes = 8;               //number of bytes at pBytes.

     printf("Initializing Winsock\n");
     ...
}

【讨论】:

  • 非常感谢您的回复。我试过了,得到了同样的错误。
  • @user2984410 VS 应该为您提供检测语法错误的确切行号。请找到那条线并告诉我们。
  • 我更新了我的代码并提到:/*ERROR IN THIS LINE*/
  • @user2984410 我知道你提到Xcp_StatePtr8的定义没有语法错误,但是你能把它的定义粘贴到你的问题中吗?
  • Xcp_StatePtr Pbytes;这种类型的实例指向地址中的 Pbytes 内存。在我的例子中:指向内存地址的 Pbytes (pbytes = &measurements ;)
【解决方案2】:

由于线路

#define port 444    //port number

声明

void XcpApp_IpTransmit(uint16 port, Xcp_StatePtr8 pBytes, uint16 numBytes)

变成了

void XcpApp_IpTransmit(uint16 444, Xcp_StatePtr8 pBytes, uint16 numBytes)

由预处理器。 444 是编译器抱怨的 'constant'

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多