Linux 'memcpy' 未在此范围内定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6090901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'memcpy' is not defined in this scope
提问by czchlong
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.
谢谢你。
采纳答案by Adam Maras
Did you include string.h/cstring (or another header that includes it) at the beginning of your code file?
您是否在代码文件的开头包含 string.h/cstring(或包含它的其他标头)?
回答by Nawaz
#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.
这似乎m_data
是char*
类型。如果是这样,那么它没有get()
功能,并且m_data.get()
在您的代码中没有意义。
An alternative solution would be using std::copy
as :
另一种解决方案是使用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
.
我更喜欢第二种解决方案。阅读std::copy
.
回答by Square
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.
我遇到了同样的问题(在头文件中),即使包含了所有正确的路径。原来我的文件名没有扩展名。将它从“array”重命名为“array.hpp”为我解决了这个问题。愚蠢的错误...容易修复。
(I'm running Eclipse Version: Juno Service Release 1, Build id: 20120920-0800 on Mac OS X 10.6.8)
(我在 Mac OS X 10.6.8 上运行 Eclipse 版本:Juno Service Release 1,构建 ID:20120920-0800)