我正在尝试使用 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/