C++ 'memcpy' 未在此范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24850479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:49:58  来源:igfitidea点击:

‘memcpy’ was not declared in this scope

c++gcc

提问by Andrea993

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

我正在尝试使用 gcc 和 eclipse 构建一个开源 C++ 库。但我收到此错误“memcpy”未在此范围内声明

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.

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

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.

我在每个 memcpy 和 memmove 函数上都会出错。

采纳答案by Flocke

You have to either put

你必须要么把

using namespace std;

to the other namespace or you do this at every memcpy or memmove:

到另一个命名空间,或者您在每个 memcpy 或 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:

此外不要忘记包含 memcpy 函数的头文件:

#include <cstring>