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

时间:2020-12-02 09:44:08

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

g++ - o端Udp。cpp ClientMain。c - i。-lpthread

In file included from ClientMain.c:1:0:

从ClientMain.c:1:0:

Udp.h: In destructor ‘CUdpMsg::~CUdpMsg()’:

Udp。析构函数的h:CUdpMsg::~ CUdpMsg():

Udp.h:103:43: error: ‘free’ was not declared in this scope

Udp。h:103:43:错误:在这个范围内没有声明“免费”。

Udp.h: In member function ‘void CUdpMsg::Add(in_addr_t, const void*, size_t)’:

Udp。h:在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:34:错误:“malloc”没有在这个范围内声明。

Udp.h:109:41: error: ‘memcpy’ was not declared in this scope

Udp。h:109:41:错误:“memcpy”没有在这个范围内声明。

ClientMain.c: In function ‘int main(int, char**)’:

ClientMain。c:在函数' int main(int, char**) '中:

ClientMain.c:28:57: error: ‘memcpy’ was not declared in this scope

ClientMain。c:28:57:错误:“memcpy”没有在这个范围内声明。

ClientMain.c:29:61: error: ‘printf’ was not declared in this scope

ClientMain。c:29:61:错误:“printf”没有在这个范围内声明。

ClientMain.c:30:17: error: ‘stdout’ was not declared in this scope

ClientMain。c:30:17:错误:“stdout”没有在这个范围内声明。

ClientMain.c:30:23: error: ‘fflush’ was not declared in this scope

ClientMain。c:30:23:错误:“fflush”没有在这个范围内声明。

ClientMain.c:34:68: error: ‘printf’ was not declared in this scope

ClientMain。c:34:68:错误:“printf”没有在这个范围内声明。

ClientMain.c:35:17: error: ‘stdout’ was not declared in this scope

ClientMain。c:35:17:错误:“stdout”没有在这个范围内声明。

ClientMain.c:35:23: error: ‘fflush’ was not declared in this scope

ClientMain。c:35:23:错误:“fflush”没有在这个范围内声明。

ClientMain.c:37:30: error: ‘usleep’ was not declared in this scope

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”这样的函数应该在字符串中声明。我有它(和字符串和cstring)都声明了,我仍然得到这些编译器错误。有人知道为什么会这样吗?谢谢。

4 个解决方案

#1


4  

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

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

#2


5  

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.

你的Udp。h文件还需要包括所需的系统标题。另外,由于您使用的是cstring和cstdlib,所以您需要使用std::因为它们不是自动导入到全局名称空间中,因此需要对所有c库函数进行限定。

#3


5  

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

Mark B详细介绍了你错误的所有原因。我只是想补充一点,您应该尽量不要在一个cpp文件中混合C头的两种风格(#include vs #include )。

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 变种将所有包含的声明引入std::名称空间。# include <头。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.

作为一个单独的问题,您遇到了一个消息头本身不包含它所需要的所有内容的情况。这可能会让调试很麻烦,因为根据包含的顺序,bug会出现或消失。

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.

如果您创建了一个头/cpp对,始终将匹配的头作为第一个包含在cpp文件中,这将保证头是完整的,并且可以独立存在。如果您创建一个不需要实现的独立消息头,您仍然可以创建一个空的.cpp来测试报头的完整性,或者仅仅通过编译器自己运行头。用你创建的每一个标题来做这个会避免像你现在这样的头痛。

#4


0  

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

一个更清洁的解决方案可能是移动CUdpMsg的实现::udp的CUdpMsg。h udp。cpp,类似于任何给你这些错误的函数。如果它们真的很简单(例如getter),则只定义header中的函数。

#1


4  

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

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

#2


5  

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.

你的Udp。h文件还需要包括所需的系统标题。另外,由于您使用的是cstring和cstdlib,所以您需要使用std::因为它们不是自动导入到全局名称空间中,因此需要对所有c库函数进行限定。

#3


5  

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

Mark B详细介绍了你错误的所有原因。我只是想补充一点,您应该尽量不要在一个cpp文件中混合C头的两种风格(#include vs #include )。

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 变种将所有包含的声明引入std::名称空间。# include <头。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.

作为一个单独的问题,您遇到了一个消息头本身不包含它所需要的所有内容的情况。这可能会让调试很麻烦,因为根据包含的顺序,bug会出现或消失。

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.

如果您创建了一个头/cpp对,始终将匹配的头作为第一个包含在cpp文件中,这将保证头是完整的,并且可以独立存在。如果您创建一个不需要实现的独立消息头,您仍然可以创建一个空的.cpp来测试报头的完整性,或者仅仅通过编译器自己运行头。用你创建的每一个标题来做这个会避免像你现在这样的头痛。

#4


0  

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

一个更清洁的解决方案可能是移动CUdpMsg的实现::udp的CUdpMsg。h udp。cpp,类似于任何给你这些错误的函数。如果它们真的很简单(例如getter),则只定义header中的函数。