Intializing ofstream in class
<p>I don't want to construct ofstream in main(). Here is what I do but it does not compile:</p>

<pre><code>#include &lt;fstream&gt;
using namespace std;
class test
{
private:
    ofstream &amp;ofs;
public:
    test(string FileName);
    void save(const string &amp;s);
};
//----------------
test::test(string FileName)
    : ofs(ofstream(FileName.c_str(),ios::out))
{
}
//----------------
void test::save(const string &amp;s)
{
    ofs &lt;&lt; s;
}
//----------------
//Provide file name and to-be-written string as arguments.
int main(int argc,char **argv)
{
    test *t=new test(argv[0]);
    t-&gt;save(argv[1]);
    delete t;
}

test.cpp: In constructor ‘test::test(std::string)’:
test.cpp:13: error: invalid initialization of non-const reference of type ‘std::ofstream&amp;’ from a temporary of type ‘std::ofstream’
</code></pre>

<p>How to fix the code?
Thank you in advance!</p>
