'memcpy' is not defined in this scope
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开发者_Python百科 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.
Did you include string.h/cstring (or another header that includes it) at the beginning of your code file?
#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
.
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)
精彩评论