C++ Boost::序列化"no matching function for call"到我的加载函数参数中类的构造函数

C++ Boost::serialization "no matching function for call" to constructor of the class in my loading function's argument

本文关键字:参数 函数 加载 我的 构造函数 call no 序列化 Boost matching function      更新时间:2023-10-16

我的实际问题是在下面的粗体文本中,但这是我问题的上下文:

我正在尝试使用Boost::序列化来保存和恢复播放器的状态。我在 http://www.boost.org/doc/libs/1_56_0/libs/serialization/doc/tutorial.html#simplecase 阅读了教程,但我不确定如何

boost::archive::text_iarchive 

工程。

我正在假设以下几行

boost::archive::text_iarchive inArchive(playerfile);
    inArchive >> target;

会根据播放器文件对应的文本文件初始化目标吗?

我根据该假设编写了以下函数:

bool SavePlayerState(Player *target)
{
std::string fileName = (target->name) + ".playerfile";
std::ofstream playerfile(fileName);
if(!playerfile.is_open())
{
    s_al_show_native_message_box(display, "SAVE FAILURE", "SAVE FAILURE", fileName + "could not be created/opened.",
                             NULL, ALLEGRO_MESSAGEBOX_ERROR);
    return false;
}
boost::archive::text_oarchive outArchive(playerfile);
outArchive << target;
return true;
}
bool LoadPlayerState(std::string playerName, Player *target)
{
std::string fileName = playerName + ".playerfile";
std::ifstream playerfile(fileName);
if(!playerfile.is_open())
{
    s_al_show_native_message_box(display, "LOAD FAILURE", "LOAD FAILURE", fileName + "could not be found/opened.",
                             NULL, ALLEGRO_MESSAGEBOX_ERROR);
    return false;
}
boost::archive::text_iarchive inArchive(playerfile);
inArchive >> target;
return true;
}

它们在 main() 中被调用以进行测试:

int main(int argc, char *argv[])
{
//...
player = new Player(5,5); // There is a Player*player; declared elsewhere
LoadPlayerState("player", player); //Load the file with this name, target is player object
beings.push_back(player);
//The game's operations...
SavePlayerState(player);
delete player;
//...
return 0;
}

SavePlayerState(player); 按我预期工作,我发现 player.playerfile 已在我的目录中创建。但是 LoadPlayerState("玩家",播放器);给我以下错误:

C:DevelopmentLibrariesboostboostserializationaccess.hpp|132|error: no matching function for call to 'Player::Player()'|

我不知道为什么构造函数会有问题,或者为什么保存有效而不是加载。我不是在尝试创建一个新的播放器对象,只是根据存档塑造现有的目标播放器。我需要更改什么才能完成这项工作?

我的玩家类别:

#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include "being.h"
#include "extfile.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/vector.hpp>
class Player: public Being
{
friend class boost::serialization::access;
template<class PlayerArchive>
void serialize(PlayerArchive & par, const unsigned int version)
{
    par & boost::serialization::base_object<Being>(*this);
    par & active;
    //par & various things
}
public:
Player(bool savedPlayer);
Player(int spawnXCell, int spawnYCell);
~Player();
//Functions
};
//Prototype to the save and Load state functions mentioned previously are found here.

#endif // PLAYER_H_INCLUDED

这是我的完整构建消息日志,如果有帮助的话:

>||=== 构建:在 Roguelike 中调试(编译器:GNU GCC 编译器)===|>C:\开发\库\boost\boost\序列化\access.hpp||在实例化"静态空隙提升::序列化::访问::构造(T*) [与 T = 玩家]":|>C:\开发\库\boost\boost\serialization\serialization.hpp|93|必需来自'void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive;T = 玩家]'|>C:\开发\库\boost\boost\serialization\serialization.hpp|158|必需来自'void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive;T = 玩家]'|>C:\Development\Libraries\boost\boost\archive\detail\iserializer.hpp|341|必需来自'void boost::archive::d etail::p ointer_iserializer::load_object_ptr(boost::archive::d etail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive;T = 玩家]'|>C:\开发\项目\Roguelike\Roguelike\player.cpp|76|从这里开始|>C:\开发\库\boost\boost\序列化\access.hpp|132|错误:调用"播放器::P layer()"没有匹配函数|>C:\开发\库\boost\boost\序列化\access.hpp|132|注意:候选者是:|>C:\开发\项目\Roguelike\Roguelike\player.cpp|8|注:播放器::P层(整数,整数)|>C:\开发\项目\Roguelike\Roguelike\player.cpp|8|注意:考生期望 2 个参数,0 提供|>C:\开发\项目\Roguelike\Roguelike\player.cpp|3|注意:玩家::P层(布尔值)|>C:\开发\项目\Roguelike\Roguelike\player.cpp|3|注意:候选人期望1个参数,0提供|>C:\开发\项目\Roguelike\Roguelike\player.h|12|注意:玩家::P层(常量玩家&)|>C:\开发\项目\Roguelike\Roguelike\player.h|12|注意:候选人期望 1 个参数,0 提供|||=== 构建失败:1 个错误、5 个警告(0 分钟、4 秒)===|

非常感谢。如果需要任何其他信息,我将附上它。

错误消息非常清楚:

调用"玩家::P layer()"没有匹配函数

类中必须具有默认构造函数。

除了 Joachim 所说的之外,在某些情况下,您将无法使类型默认可构造(尤其是当您向第三方库中的类型添加非侵入式序列化时)。

在这种情况下,请了解load_construct_data自定义点:

非默认构造函数

这种机制已经自动用于你,例如在元素类型没有默认构造函数的 STL 容器的反序列化中。