gpt4 book ai didi

c++ - cpp std::copy编译期间的问题:'struct std::iterator_traits 中没有名为 'value_type'的类型

转载 作者:行者123 更新时间:2023-12-02 10:26:40 24 4
gpt4 key购买 nike

*** 解决了 ***
在编译我编写的类,实现复制构造函数时,我遇到了问题。该类将包含所有相关数据的结构作为字段保存,我将其发送到名为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
...
}
编辑:这是类(class)的样子
#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 '中没有​​名为'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;
}

关于c++ - cpp std::copy编译期间的问题:'struct std::iterator_traits <unsigned_char>中没有名为 'value_type'的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64223302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com