‘memcpy’ was not declared in this scope

80,642

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>
Share:
80,642

Related videos on Youtube

Andrea993
Author by

Andrea993

BY DAY: Electronic and Management Engineering student BY NIGHT: Financial prediction and DSP model maker and linux user

Updated on March 04, 2021

Comments

  • Andrea993
    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
      NirMH almost 10 years
      Better to add your code to the post - it will help us help you
    • Matthieu M.
      Matthieu M. almost 10 years
      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.
    • user2357112
      user2357112 almost 10 years
      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.
    • WitchGod
      WitchGod almost 10 years
      Generally, 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
      BartoszKP almost 10 years
      Have you tried including cstring and adding using namespace std; (if this is a cpp file) (en.cppreference.com/w/cpp/string/byte/memcpy)?
    • Andrea993
      Andrea993 almost 10 years
      Yes I've already try and this is a .h file
    • NicholasM
      NicholasM almost 10 years
      The standard library contains this algorithm; it's called rotate.
    • Thomas Matthews
      Thomas Matthews almost 10 years
      OMG, 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
      jww over 6 years
  • πάντα ῥεῖ
    πάντα ῥεῖ almost 10 years
    Don't forget to mention the correct #include statement.
  • πάντα ῥεῖ
    πάντα ῥεῖ almost 10 years
    Should be #include <cstring>
  • BartoszKP
    BartoszKP almost 10 years
    OP mentions that they've tried it. Also, you should not put using namespace statements in headers.
  • Flocke
    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
    Royi almost 7 years
    What if I use gcc? How can I access memcpy?
  • Flocke
    Flocke almost 7 years
    @Royi: #include <string.h> void *memcpy(void *dest, const void *src, size_t n);
  • Royi
    Royi almost 7 years
    @Flocke, Thank You. It's funny it is needed in GCC yet not in VS.
  • skittlebiz
    skittlebiz over 2 years
    I 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
    Flocke over 2 years
    skittlebiz 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?