0

I want to make a wrapper Filer class to work with fstream library.

Thus, I want to pass an instance of fstream class through constructor of my own Filer class, which led to this code:

Filer::Filer(fstream fileObject)
{
    fileObject this->fileObj;
};

Though when I compile it, an error is thrown that :

1>Filer.cpp(10): error C2143: syntax error : missing ';' before 'this'

While when I do this:

Filer::Filer(fstream fileObject)
{
    this->fileObj = fileObject;
};

It throws this errors which complains that fstream could not be assigned in such way;

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::fstream' (or there is no acceptable conversion)

How should I then make my constructor accept an object of type fstream?

1 Answer 1

5

What you've got there is not C++. Try this:

Filer::Filer(fstream& fileObject)
  : fileObj(fileObject)
{
}

That uses an "initialization list" to store a reference to fileObject which must be declared as a member of the class. And you must use references, because streams are not copyable.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, can I ask you when in the header file we are specifying the class, what type should be assigned to fileObj ? I mean how should we declare fileObj ? string fileObj; actually does not work
@MostafaTalebi of course it doesn't work, fstream is not a string and string is not a fstream. Declare fileObj as a reference to a fstream - std::fstream& fileObj.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.