gpt4 book ai didi

C++ 编译器错误 "was not declared in this scope"

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:38 26 4
gpt4 key购买 nike

我在尝试编译 C++ UDP 客户端程序时遇到奇怪的编译器错误。

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 中声明...我已经声明了它(以及 string 和 cstring),但我仍然遇到这些编译器错误。有谁知道为什么会这样?谢谢。

最佳答案

您的Udp.h 文件 需要包含所需的系统 header 。此外,由于您使用 cstringcstdlib 作为包含,因此您需要使用 std:: 限定所有 C 库函数,因为它们不会通过这些 header 自动导入到全局命名空间中。

关于C++ 编译器错误 "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com