Compiler error: memset was not declared in this scope

C++Gcc

C++ Problem Overview


I am trying to compile my C program in Ubuntu 9.10 (gcc 4.4.1).

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:

#include <stdio.h>
#include <stdlib.h>

And the same program compiles fine under Ubuntu 8.04 (gcc 4.2.4).

Please tell me what am I missing.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichaelView Question on Stackoverflow
Solution 1 - C++sthView Answer on Stackoverflow
Solution 2 - C++Paul RView Answer on Stackoverflow