首页 文章

'memcpy'未在此范围内声明

提问于
浏览
19

我正在尝试使用gcc和eclipse构建一个开源c库 . 但我得到此错误'memcpy'未在此范围内声明

我试着包含memory.h(和string.h),如果我点击“打开声明”,eclipse会找到该函数,但gcc会给我错误 .

我能怎么做?

#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
}

我在每个memcpy和memmove函数上都出错了 .

1 回答

  • 20

    你必须要么

    using namespace std;
    

    到其他命名空间或你在每个memcpy或memmove执行此操作:

    [...]

    std::memcpy(  tmp,                buffer,              na*sizeof(T));
    

    [...]

    在您的代码中,编译器不知道在哪里查找该函数的定义 . 如果您使用命名空间,它知道在哪里找到该函数 .

    此外,不要忘记包含memcpy函数的 Headers :

    #include <cstring>
    

相关问题