Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'memcpy' is not defined in this scope

Tags:

c++

linux

memcpy

I am getting a "memcpy is not defined in this scope error" with the following piece of code:

CommonSessionMessage::CommonSessionMessage(const char* data, int size) 
    : m_data(new char[size]) {
  memcpy(m_data.get(), data, size);
}

I have looked through this site and google and could not find a solution that would resolve the issue for me.

Any assistance would be appreciated.

Thank you.

like image 562
czchlong Avatar asked May 22 '11 21:05

czchlong


2 Answers

#include <cstring>

CommonSessionMessage::CommonSessionMessage(const char* data, int size) 
: m_data(new char[size]) 
{
    std::memcpy(m_data, data, size);
}

It seems that m_data is char* type. If so, then it doesn't have get() function, and m_data.get() in your code wouldn't make sense.


An alternative solution would be using std::copy as :

#include<algorithm>

CommonSessionMessage::CommonSessionMessage(const char* data, int size) 
: m_data(new char[size]) 
{
    std::copy(data, data + size, m_data);
}

I would prefer the second solution. Read the documentation of std::copy.

like image 114
Nawaz Avatar answered Oct 16 '22 09:10

Nawaz


Did you include string.h/cstring (or another header that includes it) at the beginning of your code file?

like image 36
Adam Maras Avatar answered Oct 16 '22 08:10

Adam Maras