C++ Boost反序列化问题:运行时输入流错误(c+;+;)

C++ Boost反序列化问题:运行时输入流错误(c+;+;),c++,templates,serialization,boost,stream,C++,Templates,Serialization,Boost,Stream,我已经把地图连载好了 std::map<std::string, std::string> userandPass; saveData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass); // have removed &. why is this needed. i want to pass by reference std::map usera

我已经把地图连载好了

std::map<std::string, std::string> userandPass; 
saveData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass); // have removed &. why is this needed. i want to pass by reference
std::map userandPass;
saveData(“userandPassBackup.txt”,userandPass);//已删除&。为什么需要这样做。我想参考一下
使用函数

template <typename SaveClass>
void saveData(const std::string filename, SaveClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() / 
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() / 
filename;
// Declare an output file stream ofs that writes serialises to our 
//backup file.
boost::filesystem::ofstream ofs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_oarchive ta(ofs);
// Write to file
ta << c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed 
up to " << myFile << "\n";
}
模板
void saveData(const std::string filename,SaveClass&c)
{
//要写入的文件
boost::filesystem::remove(boost::filesystem::current_path()/
文件名);
boost::filesystem::path myFile=boost::filesystem::current_path()/
文件名;
//声明一个S的输出文件流,该流将序列化写入
//备份文件。
boost::filesystem::ofstreamofs(myFile.native());
//声明将使用输出文件流的存档ta
boost::archive::text_oarchive ta(ofs);
//写入文件

ta似乎您从
saveData
复制粘贴了
loadData
正文。您首先通过调用
boost::filesystem::remove
@VTT得到了最大的错误

以下是我的免费代码回顾:

  • 您不需要boost::filesystem来
    std::remove(filename)
  • 您应该做的不是
    currentdir/filename
  • 这已经是默认行为
  • 您应该不要使用
    native()
    ,因为您可以传递路径
  • saveData
    的参数可以是
    const&
  • 不显式传递模板参数:函数调用不进行模板参数推断
  • 别介意你的评论是多余的

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <cstdio>
#include <iostream>
#include <fstream>

template <typename SaveClass>
void saveData(const std::string filename, SaveClass const& c)
{
    std::remove(filename.c_str());
    std::ofstream ofs(filename);
    boost::archive::text_oarchive ta(ofs);

    ta << c;
}

template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
    std::ifstream ifs(filename);
    boost::archive::text_iarchive ta(ifs);
    ta >> c;
}

int main() {
    std::string filename = "userandPassBackup.txt";
    {
        std::map<std::string, std::string> userandPass {
            { "John", "pa$$w0rd" },
            { "Jane", "welcome01" } };
        saveData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully backed up to " << filename << "\n";
    }
    {
        std::map<std::string, std::string> userandPass;
        loadData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully restored from " << filename << "\n";
    }
}

我当然知道了。愚蠢的错误!非常感谢你的帮助。祝你度过愉快的一天!
template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() / 
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() / 
filename;
// Declare an output file stream ofs that writes serialises to our 
//backup file.
boost::filesystem::ifstream ifs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_iarchive ta(ifs);
// Write to file
ta >> c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed 
up to " << myFile << "\n";
}
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <cstdio>
#include <iostream>
#include <fstream>

template <typename SaveClass>
void saveData(const std::string filename, SaveClass const& c)
{
    std::remove(filename.c_str());
    std::ofstream ofs(filename);
    boost::archive::text_oarchive ta(ofs);

    ta << c;
}

template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
    std::ifstream ifs(filename);
    boost::archive::text_iarchive ta(ifs);
    ta >> c;
}

int main() {
    std::string filename = "userandPassBackup.txt";
    {
        std::map<std::string, std::string> userandPass {
            { "John", "pa$$w0rd" },
            { "Jane", "welcome01" } };
        saveData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully backed up to " << filename << "\n";
    }
    {
        std::map<std::string, std::string> userandPass;
        loadData(filename, userandPass);

        std::cout << userandPass.size() << " records from have been successfully restored from " << filename << "\n";
    }
}
2 records from have been successfully backed up to userandPassBackup.txt
2 records from have been successfully restored from userandPassBackup.txt