简体   繁体   English

Raspbian C ++错误:“结构”之前的预期主表达式

[英]Raspbian C++ Error: expected primary expression before 'struct'

I am trying to write a program for my raspberry pi that changes its system time to the time from a GPS unit on the same network. 我正在尝试为树莓派编写一个程序,以将其系统时间更改为同一网络上GPS单元的时间。 The GPS sends out a 72 byte UDP packet across port 3000. I am new to socket programming so I am unsure where I am going wrong. GPS通过端口3000发送一个72字节的UDP数据包。我是套接字编程的新手,所以我不确定我要去哪里。

The trouble that I am having is that I can't seem to get it to build with g++. 我遇到的麻烦是我似乎无法用g ++来构建它。 I am getting the following error: 我收到以下错误:

在此处输入图片说明

So the main error seems to be in the line 所以主要错误似乎在

char A = struct sockaddr_in address;

Here is the start of my program and the method where I create the socket and where the error is located, if you would like the main method of my program then I will add it too. 这是我的程序的开始,以及我创建套接字和错误所在的方法的开始,如果您想要我程序的主要方法,那么我也会添加它。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <math.h>

// defines the socket used by the GPS
#define PORT 3000 

/****************************/
 int CreateSocket(int port)
/****************************/
{
    // Create an UDP-socket
    int sock = socket(AF_INET, SOCK_DGRAM, 0);

    // Check if UDP-socket was created
    if(sock==-1)
    {
       fprintf(stderr, "1CreateSocket: socket failed\n");
       return -1;
    }

    // Bind it to the local IP-address
    struct sockaddr_in address;
    char A = struct sockaddr_in address;
    fprintf(stderr, A);

    // Pointer to the block of memory to fill with address data
    memset(&address, 0, sizeof(address));

    address.sin_family      = AF_INET;             // Address family for IP-address 
    address.sin_addr.s_addr = htonl(INADDR_ANY);   // converts the unsigned integer hostlong from host byte order to network byte order 
    address.sin_port        = htons(port);         // converts the unsigned short integer hostshort from host byte order to network byte order

    // Check if IP-address is correct, if not Socket failed. Otherwise it returns the socket
    if(bind(sock, (struct sockaddr *) &address, sizeof(address))==-1)
    {
       fprintf(stderr, "2CreateSocket: bind failed\n");
       close(sock);
       return -1;
    }
    return sock;
}

Can anyone see any obvious errors here? 有人可以在这里看到任何明显的错误吗? Thanks 谢谢

You don't really need these two lines: 您实际上并不需要以下两行:

char A = struct sockaddr_in address;
fprintf(stderr, A);

You can delete them, since they don't do anything useful, and they have a syntax error. 您可以删除它们,因为它们没有任何用处,并且存在语法错误。

And to do some extra cleanup, the comment of the binding above those lines that can be deleted should actually go above the call to bind(). 为了进行一些额外的清理,可以删除的那些行上方的绑定注释实际上应该位于对bind()的调用之上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM