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

mic*_*ael 96 c++ gcc

我试图在Ubuntu 9.10(gcc 4.4.1)中编译我的C程序.

我收到此错误:

Rect.cpp:344: error: ‘memset’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

但问题是我已经包含在我的cpp文件中:

#include <stdio.h>
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)

同样的程序在Ubuntu 8.04(gcc 4.2.4)下编译得很好.

请告诉我我错过了什么.

sth*_*sth 163

你应该包括<string.h>(或它的C++等价物<cstring>).


Pau*_*l R 129

如果您遇到这样的问题,只需转到相关功能手册页,它会告诉您缺少哪个标题,例如

$ 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);
Run Code Online (Sandbox Code Playgroud)

请注意,对于C++,通常最好使用适当的等效C++头文件,<cstring>/ <cstdio>/ <cstdlib>/ etc,而不是C的<string.h>/ <stdio.h>/ <stdlib.h>/ etc.

  • 大!你告诉我们如何钓鱼,而不是给我们一条鱼,大拇指! (35认同)