10

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.

lisyarus
  • 15,025
  • 3
  • 43
  • 68
czchlong
  • 2,434
  • 10
  • 51
  • 65

3 Answers3

22

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

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
6
#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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Sorry, I am using boost::scope_array for that. But let me give it a try and I will get back to you. Thanks. – czchlong May 22 '11 at 22:02
  • How do you know whether `m_data` has a `get()` function? We don't know its type; we only know that it's convertible to a `char*`. – TonyK May 22 '11 at 22:07
0

I was having this same problem (in a header file), even with all of the correct paths included. Turned out that my file name didn't have an extension. Renaming it from "array" to "array.hpp" solved the problem for me. Silly mistake...easy fix.

(I'm running Eclipse Version: Juno Service Release 1, Build id: 20120920-0800 on Mac OS X 10.6.8)

Square
  • 1