我正试图在Ubuntu 9.10 (gcc 4.4.1)中编译我的C程序。
但是,我遇到了这个错误:
Rect.cpp:344: error: ‘memset’ was not declared in this scope
但问题是,我已经在我的cpp文件中包含了它:
#include <stdio.h>
#include <stdlib.h>
在Ubuntu 8.04(gcc 4.2.4)下,相同的程序能够成功编译。请告诉我我错过了什么。
你应该包括<string.h>
(或其C++等效物<cstring>
)。
当你遇到这样的问题时,只需查看涉及函数的man页,它会告诉你缺少哪个头文件,例如:
$ man memset
MEMSET(3) BSD Library Functions Manual MEMSET(3)
NAME
memset -- fill a byte string with a byte value
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
void *
memset(void *b, int c, size_t len);
请注意,在C++中,通常最好使用适当的等效的C++标头,如<cstring>
/<cstdio>
/<cstdlib>
/等等,而不是C的<string.h>
/<stdio.h>
/<stdlib.h>
/等等。
<string.h>
是通过其他已包含的头文件间接#include
的。 - Paul R