c++ - 'memcpy' 未在此范围内声明

标签 c++ gcc

我正在尝试使用 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 函数都有错误。

最佳答案

你必须要么放

using namespace std;

到另一个命名空间,或者您在每个 memcpy 或 memmove 处执行此操作:

[...]

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

[...]

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

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

#include <cstring>

关于c++ - 'memcpy' 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850479/

相关文章:

c++ - 将 for 循环转换为 std::for_each

c++ - GCC 4.4/4.5 unique_ptr 不适用于 unordered_set/unordered_map

c - 为什么我们需要读写屏障?

c - 在 MSVC 中处理 __attribute__

c - 声明带有存储类说明符但没有类型说明符的变量是什么意思?

c++ - 标准字符串查找方法和计数算法的逻辑错误

c++ - Qtabwidget Tab在运行时重命名

c++ - 在cocos2dx中听不到音效

c++ - 如何在 ubuntu 14.04 中通过 apt-get 安装以前版本 (4.4.7) 的 gcc/g++?

c - 由 Matlab Coder 生成的名为 i386 的变量