简体   繁体   中英

‘memcpy’ was not declared in this scope

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.

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>

还有一种可能,当你做CP的时候,在某些平台,比如USACO,它不允许你使用memcpy因为它是C++中未经检查的操作,可能会产生严重的内存错误甚至潜在的攻击。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM