![](/img/trans.png)
[英]no type named ‘value_type’ in ‘struct std::iterator_traits
[英]cpp std::copy problems during compilation : no type named 'value_type' in 'struct std::iterator_traits<unsigned_char>
*** 解决了 ***
在实现复制构造函数时,我在编译我编写的类时遇到了问题。 该类将一个包含所有相关数据的结构作为字段保存,我将这些数据发送到一个名为 clone 的函数,该函数将结构的相关字段复制到新构造的类中。
struct tMicroControllerVersion
{
unsigned char PMC_Version [2]; // major,minor
unsigned char FPGA_Version [2];
unsigned char project_ID [2];
unsigned char configuration_Type;
unsigned char PCB_Revision;
unsigned char Unit_Serial_No [6];
unsigned char Unit_Part_No [8];
unsigned char FPGA_device_ID;
unsigned char PMC_device_ID;
unsigned char Reserved [29];
tMicroControllerVersion()
{
memset(this, 0, sizeof(tMicroControllerVersion));
}
};
我的克隆方法如下所示:
void CMicroControllerVersionMsg::clone(tMicroControllerVersion other)
{
std::copy(std::begin(other.PMC_Version),std::end(PMC_Version),this -> _data.body.PMC_Version);
std::copy(std::begin(other.FPGA_Version),std::end(other.FPGA_Version),this -> _data.body. FPGA_Version);
std::copy(std::begin(other.project_ID),std::end(other.project_ID),this -> _data.body. project_ID);
this._data.body.configuration_type = other.configuration_type
...
}
编辑:这就是班级的样子
#include <cstring>
#include "Tac4gInfras/Communication/SrcImp/BaseMessage.h"
#include "Tac4gPackages/FpgaTsComm/SrcImp/Messages/MCTsHeader.h"
#pragma pack(push, 1)
namespace FpgaTsCommNS
{
struct tMicroControllerVersion
{
unsigned char PMC_Version [2]; // major,minor
unsigned char FPGA_Version [2];
unsigned char project_ID [2];
unsigned char configuration_Type;
unsigned char PCB_Revision;
unsigned char Unit_Serial_No [6];
unsigned char Unit_Part_No [8];
unsigned char FPGA_device_ID;
unsigned char PMC_device_ID;
unsigned char Reserved [29];
tMicroControllerVersion()
{
memset(this, 0, sizeof(tMicroControllerVersion));
}
};
struct tMicroControllerVersionResp
{
/// Message data.
tMicroControllerVersion body;
};
class CMicroControllerVersionMsg: public BaseMessage
{
public:
//////////////////////////////////////////////////////////////
/// \brief Constructor
///
//////////////////////////////////////////////////////////////
CMicroControllerVersionMsg();
//////////////////////////////////////////////////////////////
/// \brief Constructor
///
//////////////////////////////////////////////////////////////
CMicroControllerVersionMsg(CMicroControllerVersionMsg* pToCopy);
//////////////////////////////////////////////////////////////
/// \brief Destructor.
///
//////////////////////////////////////////////////////////////
virtual ~CMicroControllerVersionMsg();
//////////////////////////////////////////////////////////////
/// \brief ToString for message
///
/// \param direction - direction for message
/// \return message buffer in string format
//////////////////////////////////////////////////////////////
virtual string ToString();
//////////////////////////////////////////////////////////////
/// \brief Validate message content.
///
/// \return True if message is valid, false otherwise.
//////////////////////////////////////////////////////////////
virtual bool Validate();
//////////////////////////////////////////////////////////////
/// \brief data clone
///
//////////////////////////////////////////////////////////////
void clone(tMicroControllerVersion other);
protected:
//////////////////////////////////////////////////////////////
/// \brief Get class size.
///
/// \return Derived class size.
//////////////////////////////////////////////////////////////
virtual unsigned int GetClassSize();
public:
/// data of the message
tMicroControllerVersionResp _data;
};
}//namespace FpgaTsCommNS
#pragma pack(pop)
#endif // _MICRO_CONTROLLER_VERSION_RESP_MSG_H
编译时出现错误
“在'struct std::iterator_traits<unsigned_char>'中没有名为'value_type'的类型”奇怪的是,在我的其他版本的代码中,这种对类似结构的副本的使用已经通过编译,所以我很茫然在这里做什么。 ofc 我可以手动插入 unsigned char 数组的每个值,但我正在寻找更优雅的解决方案。
注意:当我说某些东西通过编译时,我的意思是我使用 makefile 编译它,而不是一些 IDE 编译器。
编辑 2:解决了问题,使用 std::copy 应该是这样的:
std::copy(std::begin(other.FPGA_Version),std::end(other.FPGA_Version),std::begin(this -> _data.body.FPGA_Version));
经过一些重构后,它在http://cpp.sh/上编译得很好,除了以下行:
this._data.body.configuration_type = other.configuration_type
这是一个指针所以使用'->'
struct tMicroControllerVersion
{
unsigned char PMC_Version [2]; // major,minor
unsigned char FPGA_Version [2];
unsigned char project_ID [2];
unsigned char configuration_Type;
unsigned char PCB_Revision;
unsigned char Unit_Serial_No [6];
unsigned char Unit_Part_No [8];
unsigned char FPGA_device_ID;
unsigned char PMC_device_ID;
unsigned char Reserved [29];
tMicroControllerVersion()
{
memset(this, 0, sizeof(tMicroControllerVersion));
}
void clone(tMicroControllerVersion other);
};
void tMicroControllerVersion::clone(tMicroControllerVersion other)
{
std::copy(std::begin(other.PMC_Version),std::end(PMC_Version),this->PMC_Version);
std::copy(std::begin(other.FPGA_Version),std::end(other.FPGA_Version),this->FPGA_Version);
std::copy(std::begin(other.project_ID),std::end(other.project_ID),this->project_ID);
this->configuration_Type = other.configuration_Type;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.