简体   繁体   中英

'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 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.

您是否在代码文件的开头包含string.h / cstring(或包含它的其他标头)?

#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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM