samedi 25 avril 2015

Operator << overload for ofstream


I would appreciate if you could take a look and tell me what im doing wrong. I want to overload an operator for writing object values into a file. Here is what i came up with.

template<typename T>ofstream &operator<<(ofstream &f, const config<T> &X)  
{
    if(X.attribName1.size()!=0)
    {f<<X.attribName1;
for(int i=0;i<3;i++)
        {

            f<<X.attribute1[i];
}}

return f;

}

Implementation :

FILE1NAME="Config_file_no1";
ofstream file1(FILE1NAME+".ini");
    ofstream file2;

    config<double> first;
    first.setAttribName1(file1name1);
    first.setAtt1(val2);


    file1<<first;

The file gets created but its empty. Anyone has any suggestions ? Im thankful for any help.


Aucun commentaire:

Enregistrer un commentaire