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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6748569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:36:48  来源:igfitidea点击:

c++ compiler error "was not declared in this scope"

c++g++compiler-errors

提问by Mike

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

尝试编译 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

g++ -o 客户端 Udp.cpp ClientMain.c -I. -lpthread

在 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' 未在此范围内声明

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

我在 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>

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.

像“memcpy”这样的函数应该在 string.h 中声明......我已经声明了它(以及字符串和 cstring),但我仍然收到这些编译器错误。有谁知道为什么会这样?谢谢。

采纳答案by dchhetri

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

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

回答by Mark B

Your Udp.hfile alsoneeds to include the needed system headers. Additionally, since you use cstringand cstdlibas your includes, you'll need to qualify all the C-library functions with std::since they aren'tautomatically imported into the global namespace by those headers.

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

回答by Greg Howell

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>).

马克 B 涵盖了您错误的所有确切原因。我只想补充一点,您应该尽量不要在单个 cpp 文件(#include <cHEADER>vs #include <HEADER.h>)中混合两种风格的 C 头文件。

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.

#include <cHEADER>变体将所有包含的声明带入 std:: 命名空间。该#include <HEADER.h>文件包含的声明没有。在需要时维护代码很烦人,std::malloc()但是::strncpy()。为每个文件选择一种方法,或者更好地为整个项目选择一种方法。

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.

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

回答by MSalters

A cleaner solution is probably to move the implementation of CUdpMsg::~CUdpMsgfrom udp.hto 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).

更简洁的解决方案可能是将CUdpMsg::~CUdpMsgfrom的实现移动udp.hudp.cpp,以及类似的任何给您此类错误的函数。仅在头文件中定义函数,如果它们真的很简单(例如 getter)。