Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘memcpy’ was not declared in this scope

Tags:

c++

gcc

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

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.

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.

like image 475
Andrea993 Avatar asked Jul 20 '14 12:07

Andrea993


People also ask

How do you declare memcpy in C++?

The prototype of memcpy() as defined in the cstring header file is: void* memcpy(void* dest, const void* src,size_t count); When we call this function, it copies count bytes from the memory location pointed to by src to the memory location pointed to by dest .

Where is defined memcpy?

memcpy is declared in the standard header <string. h> (or <cstring> in C++). Its definition depends on the implementation, and you ordinarily shouldn't need to care about that. As long as you have the proper #include directive, the compiler and linker should take care of finding it for you.

Why do we need memcpy?

memcpy() is specifically designed to copy areas of memory from one place to another so it should be as efficient as the underlying architecture will allow.

Can memcpy overlap?

The CRT function memcpy doesn't support overlapping memory.


1 Answers

You have to either put

using namespace std; 

to the other namespace or you do this at every memcpy or 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:

#include <cstring> 
like image 92
Flocke Avatar answered Oct 05 '22 22:10

Flocke