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
我的实际问题是在下面的粗体文本中,但这是我问题的上下文:
我正在尝试使用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 容器的反序列化中。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类