简体   繁体   English

C ++错误,此声明没有存储类或类型说明符

[英]C++ Error this declaration has no storage class or type specifier

I'm trying to setup a timeout for the select(int, fd_set, fd_set) function for UDP socket connections. 我正在尝试为UDP套接字连接的select(int, fd_set, fd_set)函数设置超时。

When i setup the second and usecond variables, I get Error this declaration has no storage class or type specifier . 当我设置第二个和第二个变量时,出现错误,此声明没有存储类或类型说明符

Here's the code 这是代码

#define UTIMER 300000
#define STIMER 0 
struct timeval timeouts;
timeouts.tv_sec=STIMER;    // <-- ERROR HERE
timeouts.tv_usec=UTIMER;   // <-- ERROR HERE

The problem is that you haven't #include d the header which defines timeval. 问题是您尚未#include d定义了timeval的标头。 The struct timeval timeouts is essentially a prototype declaration. struct timeval timeouts本质上是一个原型声明。 It provides enough information for the compiler to know the variable exists and allow you to, for example, use it in pointer operations, with type information about the pointer (that it points to a struct timeval ). 它为编译器提供了足够的信息,以使其知道变量的存在,并允许您在指针操作中使用它,以及有关指针的类型信息(它指向struct timeval )。

But it doesn't yet know what the inside of it looks like. 但是它还不知道它的内部是什么样。

If this is Windows, you need to #include <Winsock2.h> ; 如果是Windows,则需要#include <Winsock2.h> ; Linux #include <sys/time.h> Linux #include <sys/time.h>

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

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