c++怎样实现在文件给对象赋值?

想把文件的读取和关闭写在类的构造函数和析构函数内,一直报错,有没有解决办法?

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class god
{
    public:
        string name;
        string weapon;
        int age;
        string call;
        god()
        {

            ifstream fin("sum.in");
            ofstream fout("sum.out");
            fin>>name>>weapon>>age>>call;   
        }
        ~god()
        {
            fin.close();
            fout.close();
        }
        void show()
        {
            fout<<"吾乃"<<name<<",擅长使用"<<weapon<<",修行"<<age<<"年,"<<"号"<<call;
        }


};
int main()
{
    god swk;
    swk.show(); 
    return 0;
}

把fin和fout写做类的成员
代码如下:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class god
{
public:
    string name;
    string weapon;
    int age;
    string call;
    ifstream fin;
    ofstream fout;
    god()
    {

        fin.open("sum.in");
        fout.open("sum.out");
        fin>>name>>weapon>>age>>call;
    }
    ~god()
    {
        fin.close();
        fout.close();
    }
    void show()
    {
        fout<<"吾乃"<<name<<",擅长使用"<<weapon<<",修行"<<age<<"年,"<<"号"<<call;
    }


};
int main()
{
    god swk;
    swk.show();
    return 0;
}

望采纳