File Input and Output using YAML files and static function
Hi, In this tutorial about yaml files function read and write are static.If you want to write your own data structures in an application with many source files static function is not very pratical. Is it really necessary to have static function? I try to write my own program which is made of three files B.h my class B, B.cpp read, write and operator << and main testYAML.cpp
and main
#include <opencv2/opencv.hpp>
#include <iostream>
#include <map>
#include <fstream>
#include "B.h"
using namespace cv;
using namespace std;
int main (int argc,char **argv)
{
vector< Point> p;
B x,y;
x.entier["one"]=1;
x.entier["two"]=2;
x.reel["pi"]=acos(-1.0);
x.reel["e"]=exp(1);
{
FileStorage fs("test.yml",FileStorage::WRITE);
fs<<"operation"<<x;
x.write(fs);
}
FileStorage fs("test.yml",FileStorage::READ);
//y.read(fs);
// fs<<"Class B"<<x; I don't know how to do this
};
I cannot use something like this fs<<"CLASSB"<<x because="" i="" have="" got="" an="" error="" (vs2013="" opencv="" 3.0)<="" p="">
(error C2665: 'cv::write' : aucune des 9 surcharges n'a pu convertir tous les types d'arguments) error C2665: 'cv::write' cannot convert argument
Thanks for your answer
B.h is
#ifndef __CLASS_B__
#define __CLASS_B__
#include <opencv2/opencv.hpp>
#include <iostream>
#include <map>
#include <fstream>
class B {
public :
explicit B(){};
std::map< cv::String,int> entier;
std::map< cv::String,double> reel;
void write(cv::FileStorage& fs) const; //Write serialization for this class
void read(const cv::FileNode& node); //Read serialization for this class
};
std::ostream& operator<<(std::ostream& out, const B& m);
#endif
B.cpp is
#include "B.h"
using namespace cv;
using namespace std;
void B::write(cv::FileStorage& fs) const //Write serialization for this class
{
int i=0;
for ( auto iti = entier.begin();iti!=entier.end();iti++,i++)
{
char tmp[256];
sprintf(tmp,"%d",i);
String s("keyInt");
s += tmp;
fs<<s<<iti->first;
s= "ValueInt";
s += tmp;
fs<<s<<iti->second;
}
i=0;
for ( auto iti = reel.begin();iti!=reel.end();iti++,i++)
{
char tmp[256];
sprintf(tmp,"%d",i);
String s("keyReel");
s += tmp;
fs<<s<<iti->first;
s= "ValueReel";
s += tmp;
fs<<s<<iti->second;
}
}
void B::read(const cv::FileNode& node) //Read serialization for this class
{
char tmp[256];
int i=0,val;
double x;
String s,key;
do
{
sprintf(tmp,"%d",i);
s="keyInt";
s += tmp;
if(!node[s].empty())
{
key=(string)node[s];
s= "ValueInt";
s += tmp;
if(!node[s].empty())
{
val=(int)node[s];
entier[key]=val;
}
}
}
while (!node[s].empty());
do
{
sprintf(tmp,"%d",i);
s="keyReel";
s += tmp;
if(!node[s].empty())
{
key=(string)node[s];
s= "ValueReel";
s += tmp;
if(!node[s].empty())
{
x=(double)node[s];
entier[key]=val;
}
}
}
while (!node[s].empty());
}
static void write(FileStorage& fs, const std::string&, const B& x)
{
x.write(fs);
}
static void read(const FileNode& node, B& x, const B& default_value = B()){
if(node.empty())
x = default_value;
else
x.read(node);
}
ostream& operator ...