26
votes

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.

2
Better to add your code to the post - it will help us help youNirMH
Well, 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.Matthieu M.
My 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.user2357112 supports Monica
Have you tried including cstring and adding using namespace std; (if this is a cpp file) (en.cppreference.com/w/cpp/string/byte/memcpy)?BartoszKP

2 Answers

32
votes

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>
0
votes

There's also one possibility, when you do CP and in some platforms, such as USACO, it doesn't allow you to use memcpy because it's an unchecked operation in C++, which could produce serious memory errors and even potential attacks.