简体   繁体   中英

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.

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. The struct timeval timeouts is essentially a prototype declaration. 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 ).

But it doesn't yet know what the inside of it looks like.

If this is Windows, you need to #include <Winsock2.h> ; Linux #include <sys/time.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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