编译器错误: memset 未在此范围中声明

我尝试在 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)下编译得很好。

请告诉我我错过了什么。

183084 次浏览

You should include <string.h> (or its C++ equivalent, <cstring>).

Whevever you get a problem like this just go to the man page for the function in question and it will tell you what header you are missing, e.g.

$ 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);

Note that for C++ it's generally preferable to use the proper equivalent C++ headers, <cstring>/<cstdio>/<cstdlib>/etc, rather than C's <string.h>/<stdio.h>/<stdlib.h>/etc.