我正在尝试用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函数上都出错了。
答案 0 :(得分:20)
你必须要么
using namespace std;
到另一个命名空间,或者你在每个memcpy或memmove上执行此操作:
[...]
std::memcpy( tmp, buffer, na*sizeof(T));
[...]
在您的代码中,编译器不知道在哪里查找该函数的定义。如果您使用命名空间,它知道在哪里找到该函数。
此外,不要忘记包含memcpy函数的标题:
#include <cstring>
答案 1 :(得分:0)
还有一种可能,当你做CP的时候,在某些平台,比如USACO,不允许你使用memcpy
,因为它是C++中的未检查操作,可能会产生严重的内存错误,甚至潜在的攻击。</p>