简体   繁体   English

'memcpy'未在此范围内定义

[英]'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: 我使用以下代码获得“memcpy未在此范围错误中定义”:

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. 似乎m_datachar*类型。 If so, then it doesn't have get() function, and m_data.get() in your code wouldn't make sense. 如果是这样,那么它没有get()函数,并且代码中的m_data.get()没有意义。


An alternative solution would be using std::copy as : 另一种解决方案是使用std::copy作为:

#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 . 阅读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. 将它从“数组”重命名为“array.hpp”为我解决了这个问题。 Silly mistake...easy fix. 愚蠢的错误...容易解决。

(I'm running Eclipse Version: Juno Service Release 1, Build id: 20120920-0800 on Mac OS X 10.6.8) (我正在运行Eclipse版本:Juno Service Release 1,在Mac OS X 10.6.8上构建id:20120920-0800)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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