想把文件的读取和关闭写在类的构造函数和析构函数内,一直报错,有没有解决办法?
#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;
}