C++ 编译器错误:未在此范围内声明 memset
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2505365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiler error: memset was not declared in this scope
提问by michael
I am trying to compile my C program in Ubuntu 9.10 (gcc 4.4.1).
我正在尝试在 Ubuntu 9.10 (gcc 4.4.1) 中编译我的 C 程序。
I am getting this error:
我收到此错误:
Rect.cpp:344: error: ‘memset' was not declared in this scope
But the problem is I have already included in my cpp file:
但问题是我已经包含在我的 cpp 文件中:
#include <stdio.h>
#include <stdlib.h>
And the same program compiles fine under Ubuntu 8.04 (gcc 4.2.4).
同样的程序在 Ubuntu 8.04 (gcc 4.2.4) 下编译得很好。
Please tell me what am I missing.
请告诉我我错过了什么。
回答by sth
You should include <string.h>
(or its C++ equivalent, <cstring>
).
您应该包括<string.h>
(或其 C++ 等效项,<cstring>
)。
回答by Paul R
Whevever you get a problem like this just go to the man page for the functionin 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.
请注意,对于 C++,通常最好使用正确的等效 C++ 头文件<cstring>
/ <cstdio>
/ <cstdlib>
/etc,而不是 C 的<string.h>
/ <stdio.h>
/ <stdlib.h>
/etc。