c++编译器错误“未在此范围内声明”

发布于 2024-11-24 21:22:25 字数 1347 浏览 0 评论 0原文

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

g++ -o 客户端 Udp.cpp ClientMain.c -I。 -lp线程

在 ClientMain.c:1:0 包含的文件中:

Udp.h:在析构函数“CUdpMsg::~CUdpMsg()”中:

Udp.h:103:43:错误:在此范围内未声明“free”

Udp.h:在成员函数“void CUdpMsg::Add(in_addr_t, const void*, size_t)”中:

Udp.h:109:34:错误:“malloc”未在此范围内声明

Udp.h:109:41:错误:“memcpy”未在此范围内声明

ClientMain.c:在函数“int main(int, char**)”中:

ClientMain.c:28:57:错误:“memcpy”未在此范围内声明

ClientMain.c:29:61:错误:“printf”未在此范围内声明

ClientMain.c:30:17:错误:“stdout”未在此范围内声明

ClientMain.c:30:23: 错误:未在此范围内声明“fflush”

ClientMain.c:34:68:错误:“printf”未在此范围内声明

ClientMain.c:35:17:错误:“stdout”未在此范围内声明

ClientMain.c:35:23:错误:“fflush”未在此范围内声明

ClientMain.c:37:30: 错误:“usleep”未在此范围内声明

我在 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),但我仍然收到这些编译器错误。有谁知道为什么会发生这种情况?谢谢。

I'm getting a bizarre compiler error when trying to compile a c++ UDP client program.

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

I have the following declared at the beginning of my cpp file.

#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>

functions like 'memcpy' should be declared in string.h... I have it (and string and cstring) all declared, and I'm still getting these compiler errors. Does anyone have a clue why this is happening? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯寂 2024-12-01 21:22:25

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

Your Udp.h file also needs to include the needed system headers. Additionally, since you use cstring and cstdlib as your includes, you'll need to qualify all the C-library functions with std:: since they aren't automatically imported into the global namespace by those headers.

栀梦 2024-12-01 21:22:25

标记 B 涵盖了导致错误的所有确切原因。我只是想补充一点,您应该尽量不要在单个 cpp 文件中混合两种风格的 C 标头(#include#include< /代码>)。

#include 变量将所有包含的声明带入 std:: 命名空间。 #include 文件包含不包含的声明。当您需要 std::malloc() 但需要 ::strncpy() 时,维护代码很烦人。为每个文件选择一种方法,或者更好的是为整个项目选择一种方法。

作为一个单独的问题,您遇到了标头本身不包含其所需的所有内容的情况。这对于调试来说可能很烦人,因为错误可能会出现或消失,具体取决于包含顺序。

如果创建标头/cpp 对,请始终将匹配的标头作为 cpp 文件中的第一个包含,这将保证标头完整且可以独立存在。如果您创建不需要实现的独立标头,您仍然可以创建一个空的 .cpp 来测试标头的包含完整性,或者只是通过编译器本身运行标头。对您创建的每个标头执行此操作将防止像当前那样令人头痛。

Mark B covered all the exact causes of your error. I just want to add that you should try not to mix the two flavors of C headers in a single cpp file (#include <cHEADER> vs #include <HEADER.h>).

The #include <cHEADER> variety brings all of the included declarations into the std:: namespace. The #include <HEADER.h> files include declarations do not. It is annoying to maintain code when you need std::malloc() but ::strncpy(). Pick one approach for each file or, more preferably, one approach for your entire project.

As a separate issue, you've encountered a situation in which a header does not itself include everything it needs. This can be annoying to debug because bugs can appear or disappear depending on include ordering.

If you create a header/cpp pair, always make the matched header the first include in the cpp file, this will guarantee that the header is complete and can stand on its own. If you create a standalone header that needs no implementation, you can still create an empty .cpp to test the header for include completeness, or just run the header through your compiler by itself. Doing this with every header you create will prevent headaches like your current one.

风筝在阴天搁浅。 2024-12-01 21:22:25

如果您有多个文件,则需要在每个文件中包含适当的内容。也可能它不在命名空间内?

If you have multiple files, then you need the appropriate includes in each file. Also maybe its not within namespace?

野侃 2024-12-01 21:22:25

更简洁的解决方案可能是将 CUdpMsg::~CUdpMsg 的实现从 udp.h 移至 udp.cpp,类似地,任何函数给你这样的错误。仅当函数非常简单时才在标头中定义它们(例如 getter)。

A cleaner solution is probably to move the implementation of CUdpMsg::~CUdpMsg from udp.h to udp.cpp, and similarly any function that is giving you such errors. Only define functions in headers if they're really simple (e.g. getters).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文