当我尝试编译一个C++ UDP客户端程序时,出现了一个奇怪的编译器错误。
以下是我的代码:g++ -o client Udp.cpp ClientMain.c -I. -lpthread
在文件ClientMain.c中包含了Udp.h,Udp.h中有一些问题:
1. 在析构函数“CUdpMsg::~CUdpMsg()”中,出现了错误:“error: 'free' was not declared in this scope”。
2. 在成员函数“void CUdpMsg::Add(in_addr_t, const void*, size_t)”中出现了错误:“error: 'malloc' was not declared in this scope”和“error: 'memcpy' was not declared in this scope”。
除此之外,在主函数“int main(int, char**)”中还有一些错误:
1. 在第28行出现了错误:“error: 'memcpy' was not declared in this scope”。
2. 在第29行出现了错误:“error: 'printf' was not declared in this scope”。
3. 在第30行和第35行出现了错误:“error: 'stdout' was not declared in this scope”。
4. 在第30行和第35行出现了错误:“error: 'fflush' was not declared in this scope”。
5. 在第34行出现了错误:“error: 'printf' was not declared in this scope”。
6. 在第37行出现了错误:“error: 'usleep' was not declared in this scope”。
我在cpp文件开头声明了以下内容。
以下是我的代码:g++ -o client Udp.cpp ClientMain.c -I. -lpthread
在文件ClientMain.c中包含了Udp.h,Udp.h中有一些问题:
1. 在析构函数“CUdpMsg::~CUdpMsg()”中,出现了错误:“error: 'free' was not declared in this scope”。
2. 在成员函数“void CUdpMsg::Add(in_addr_t, const void*, size_t)”中出现了错误:“error: 'malloc' was not declared in this scope”和“error: 'memcpy' was not declared in this scope”。
除此之外,在主函数“int main(int, char**)”中还有一些错误:
1. 在第28行出现了错误:“error: 'memcpy' was not declared in this scope”。
2. 在第29行出现了错误:“error: 'printf' was not declared in this scope”。
3. 在第30行和第35行出现了错误:“error: 'stdout' was not declared in this scope”。
4. 在第30行和第35行出现了错误:“error: 'fflush' was not declared in this scope”。
5. 在第34行出现了错误:“error: 'printf' was not declared in this scope”。
6. 在第37行出现了错误:“error: 'usleep' was not declared in this scope”。
我在cpp文件开头声明了以下内容。
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <cstdlib>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <errno.h>
'memcpy'等函数应该在string.h中声明...我已经将所有头文件包括string.h、string和cstring声明,但仍然出现编译器错误。有人知道这是为什么吗?谢谢。
ClientMain.c
(注意是“.c”,不是“.cpp”)? - Georg FritzscheUDP.h
中包含一些那些文件。 - Djolestd::malloc
。 - MSalters<stdlib.h>
和<cstdlib>
不是一个好的实践。 - triclosan