我在編譯C++ UDP客戶端程序時遇到了一個奇怪的編譯器錯誤。C++編譯器錯誤「未在此範圍內聲明」
g++ -o client Udp.cpp ClientMain.c -I. -lpthread
In file included from ClientMain.c:1:0:
Udp.h: In destructor ‘CUdpMsg::~CUdpMsg()’:
Udp.h:103:43: error: ‘free’ was not declared in this scope
Udp.h: In member function ‘void CUdpMsg::Add(in_addr_t, const void*, size_t)’:
Udp.h:109:34: error: ‘malloc’ was not declared in this scope
Udp.h:109:41: error: ‘memcpy’ was not declared in this scope
ClientMain.c: In function ‘int main(int, char**)’:
ClientMain.c:28:57: error: ‘memcpy’ was not declared in this scope
ClientMain.c:29:61: error: ‘printf’ was not declared in this scope
ClientMain.c:30:17: error: ‘stdout’ was not declared in this scope
ClientMain.c:30:23: error: ‘fflush’ was not declared in this scope
ClientMain.c:34:68: error: ‘printf’ was not declared in this scope
ClientMain.c:35:17: error: ‘stdout’ was not declared in this scope
ClientMain.c:35:23: error: ‘fflush’ was not declared in this scope
ClientMain.c:37:30: 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中聲明......我把它(和字符串和CString的)所有申報,而我仍然得到這些編譯器錯誤。有沒有人有線索爲什麼會發生這種情況?謝謝。
你說你在你的*「cpp」*文件中包含這些內容,但錯誤在'ClientMain.c'中(注意'.c',而不是'.cpp')? –
我想你需要在'UDP.h'中包含這些文件中的一部分 – Djole
是你爲這個函數調用的std命名空間 – triclosan