C ++ Boost :: serialization“没有匹配函数用于调用”到我的loading函数参数中的类的构造函数

时间:2014-11-20 10:51:37

标签: c++ serialization boost

我的实际问题在下面的粗体文字中,但这是我问题的背景:

我尝试使用Boost :: serialization来保存和恢复玩家的状态。我在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.playerfile。但是LoadPlayerState(&#34;播放器&#34;,播放器);给我以下错误:

C:\Development\Libraries\boost\boost\serialization\access.hpp|132|error: no matching function for call to 'Player::Player()'|

我不知道为什么构造函数应该有问题,或者为什么Save工作而不是Load。我没有尝试制作新的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

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

>||=== Build: Debug in Roguelike (compiler: GNU GCC Compiler) ===|
>C:\Development\Libraries\boost\boost\serialization\access.hpp||In instantiation of 'static void boost::serialization::access::construct(T*) [with T = Player]':|
>C:\Development\Libraries\boost\boost\serialization\serialization.hpp|93|required from 'void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Libraries\boost\boost\serialization\serialization.hpp|158|required from 'void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Libraries\boost\boost\archive\detail\iserializer.hpp|341|required from 'void boost::archive::detail::pointer_iserializer::load_object_ptr(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|76|required from here|
>C:\Development\Libraries\boost\boost\serialization\access.hpp|132|error: no matching function for call to 'Player::Player()'|
>C:\Development\Libraries\boost\boost\serialization\access.hpp|132|note: candidates are:|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|8|note: Player::Player(int, int)|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|8|note:   candidate expects 2 arguments, 0 provided|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|3|note: Player::Player(bool)|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|3|note:   candidate expects 1 argument, 0 provided|
>C:\Development\Projects\Roguelike\Roguelike\player.h|12|note: Player::Player(const Player&)|
>C:\Development\Projects\Roguelike\Roguelike\player.h|12|note:   candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 5 warning(s) (0 minute(s), 4 second(s)) ===|

非常感谢。如果需要任何其他信息,我会追加它。

2 个答案:

答案 0 :(得分:1)

错误信息非常清楚:

no matching function for call to 'Player::Player()'

您的班级必须有默认构造函数。

答案 1 :(得分:1)

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

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

  

<强> Non-default Constructors

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