Reputation:
I have a strange compilation error when using:
#include <string.h>
in .cpp file:
/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)
while putting the same line code in .h file, does not give any errors.
I'm confused, what is the problem ?
Upvotes: 0
Views: 3359
Reputation: 965
I don't know exactly why you'd be getting that error, but in general the use of <string.h>
is deprecated for C++, so although it often works, you can't expect it to. <string>
is the C++ header that contains the string class defined in the std
namespace.
<string.h>
contains the C string functions and definitions. In C++, if you want those older functions you should use <cstring>
. I think if you mix things around in C++ you'll get namespace issues and such, which may be causing your error.
Upvotes: 0
Reputation: 385204
It is impossible for there to be a difference here between including the header from a .h
and from a .cpp
. Literally impossible. Next time, post a testcase (I cannot emphasise how important this is).
However, you are probably including some other header in your .cpp
, which defines size_t
, masking the bug. Yes, this is a bug. Standard headers should work in isolation and, apparently, on your implementation, this one doesn't.
However, since you should be including C++ <cstring>
, not C <string.h>
, it's moot.
If you do that and still have a problem, then you messed something up. And we cannot tell what that is without a testcase.
Upvotes: 1