‘memcpy’ was not declared in this scope
You have to either put
using namespace std;
to the other namespace or you do this at every memcpy or memmove:
[...]
std::memcpy( tmp, buffer, na*sizeof(T));
[...]
in your code the compiler doesnt know where to look for the definition of that function. If you use the namespace it knows where to find the function.
Furthermore dont forget to include the header for the memcpy function:
#include <cstring>
Related videos on Youtube
Andrea993
BY DAY: Electronic and Management Engineering student BY NIGHT: Financial prediction and DSP model maker and linux user
Updated on March 04, 2021Comments
-
Andrea993 about 3 years
I'm trying to build an open source c++ library with gcc and eclipse. But I get this error ‘memcpy’ was not declared in this scope
I've try to include memory.h (and string.h) and eclipse find the function if I click "open declaration" but gcc give me the error.
How can I do?
#include <algorithm> #include <memory.h> namespace rosic { //etc etc template <class T> void circularShift(T *buffer, int length, int numPositions) { int na = abs(numPositions); while( na > length ) na -=length; T *tmp = new T[na]; if( numPositions < 0 ) { memcpy( tmp, buffer, na*sizeof(T)); memmove( buffer, &buffer[na], (length-na)*sizeof(T)); memcpy( &buffer[length-na], tmp, na*sizeof(T)); } else if( numPositions > 0 ) { memcpy( tmp, &buffer[length-na], na*sizeof(T)); memmove(&buffer[na], buffer, (length-na)*sizeof(T)); memcpy( buffer, tmp, na*sizeof(T)); } delete[] tmp; } //etc etc }
I get error on each memcpy and memmove function.
-
NirMH almost 10 yearsBetter to add your code to the post - it will help us help you
-
Matthieu M. almost 10 yearsWell, to start with you will need to provide more context on the options with which you invoke gcc and what the exact error is (filename, extract of the code not compiling, copy/pasting the exact error). Normally
string.h
is bundled with gcc and does not require any additional option, so there is something fishy here. -
user2357112 almost 10 yearsMy guess is you mistyped
#include <string.h>
or put it too low in your file. Show us some minimal example code that demonstrates the problem. -
WitchGod almost 10 yearsGenerally, if you're compiling C++ code, you should be using g++ and not gcc. There are a few subtle differences. This answer shows a small comparison between the two. Of note is the third point. Give it a try and let us know. Otherwise, post your code.
-
BartoszKP almost 10 yearsHave you tried including
cstring
and addingusing namespace std;
(if this is a cpp file) (en.cppreference.com/w/cpp/string/byte/memcpy)? -
Andrea993 almost 10 yearsYes I've already try and this is a .h file
-
NicholasM almost 10 yearsThe standard library contains this algorithm; it's called
rotate
. -
Thomas Matthews almost 10 yearsOMG, why are you moving data? You should be changing indices or pointers instead. Try declaring an array and using the formula
next_index = (previous_index + 1) % ARRAY_CAPACITY;
. Much faster, less code than copying each element. -
jww over 6 yearsPossible duplicate of What header should I include for memcpy and realloc?
-
-
πάντα ῥεῖ almost 10 yearsDon't forget to mention the correct
#include
statement. -
πάντα ῥεῖ almost 10 yearsShould be
#include <cstring>
-
BartoszKP almost 10 yearsOP mentions that they've tried it. Also, you should not put
using namespace
statements in headers. -
Flocke almost 9 years@BartoszKP: i think you should never have namespaces in headers... if you are using/want to use the header in some other file you will have the namespace, which you probably wont reconize (e.g. deeply nested header inclusion)
-
Royi almost 7 yearsWhat if I use
gcc
? How can I accessmemcpy
? -
Flocke almost 7 years@Royi:
#include <string.h> void *memcpy(void *dest, const void *src, size_t n);
-
Royi almost 7 years@Flocke, Thank You. It's funny it is needed in GCC yet not in VS.
-
skittlebiz over 2 yearsI tried using std::, but I got response that memcpy_s is not a member of std (I'm using memcpy with _s though (so if someone can help me with memcpy_s that would be nice).)
-
Flocke over 2 yearsskittlebiz memcpy_s is added since C++11 dont know if you are using something older maybe? Are you compiling in gcc oder visual studio compiler or what?