简体   繁体   English

'memcpy' 未在此范围内声明

[英]‘memcpy’ was not declared in this scope

I'm trying to build an open source c++ library with gcc and eclipse.我正在尝试使用 gcc 和 eclipse 构建一个开源 C++ 库。 But I get this error 'memcpy' was not declared in this scope但我收到此错误“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 函数上都会出错。

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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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