[英]#include <afxinet.h> in a header file causes a lot of compilation error, however, include it in a cpp file is ok
[英]Why including <string.h> in .cpp file causes compilation errors (while including it .h file is ok)
使用时出现奇怪的编译错误:
#include <string.h>
在.cpp文件中:
/usr/include/string.h:47:8: error: ‘size_t’ has not been declared
size_t __n) __THROW __nonnull ((1, 2));
^
/usr/include/string.h:50:56: error: ‘size_t’ has not been declared
extern void *memmove (void *__dest, const void *__src, size_t __n)
^
/usr/include/string.h:59:18: error: ‘size_t’ has not been declared
int __c, size_t __n)
^
/usr/include/string.h:66:42: error: ‘size_t’ has not been declared
extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1)); /usr/include/string.h:47:8: error: ‘size_t’ has not been declared
size_t __n) __THROW __nonnull ((1, 2));
^
/usr/include/string.h:50:56: error: ‘size_t’ has not been declared
extern void *memmove (void *__dest, const void *__src, size_t __n)
^
/usr/include/string.h:59:18: error: ‘size_t’ has not been declared
int __c, size_t __n)
^
/usr/include/string.h:66:42: error: ‘size_t’ has not been declared
extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
^
/usr/include/string.h:69:56: error: ‘size_t’ has not been declared
extern int memcmp (const void *__s1, const void *__s2, size_t __n)
^
/usr/include/string.h:69:56: error: ‘size_t’ has not been declared
extern int memcmp (const void *__s1, const void *__s2, size_t __n)
在.h文件中放置相同的行代码时,不会出现任何错误。
我很困惑,这是什么问题?
在这里包括.h
和.cpp
的标头之间没有区别是不可能的。 从字面上看是不可能的。 下次,发布一个测试用例 (我不能强调它的重要性)。
但是,您可能在.cpp
包括其他一些头文件 ,该文件头定义了size_t
,从而掩盖了该错误。 是的,这是一个错误。 标准标头应独立工作,显然,在您的实现中,这一点不行。
但是,由于您应该包括C ++ <cstring>
,而不是C <string.h>
,因此很无聊。
如果这样做仍然有问题,那么您就搞砸了。 没有测试用例,我们无法分辨出什么。
我不确切知道为什么会收到该错误,但是通常不建议在C ++中使用<string.h>
,因此尽管它经常有效,但您不能期望它会发生。 <string>
是C ++标头,其中包含在std
名称空间中定义的字符串类。
<string.h>
包含C字符串函数和定义。 在C ++中,如果需要这些较旧的功能,则应使用<cstring>
。 我认为,如果在C ++中混合使用各种内容,则会遇到名称空间等问题,这可能会导致您的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.