Laserbeak43
Laserbeak43

Reputation: 609

QFile takes more than one parameter?

i have a class:

Class MyClass
{
    void myMember();
    ///code etc
    private:
        QFile fileMBox; 
}

and in the class' member i try to use:

void MyClass::myMember()
{
    fileMBox ("myFile.txt");
}

and i get an error saying: "error: C2064: term does not evaluate to a function taking 1 arguments" but the docs say to use:

QFile file("in.txt");

what am i doing wrong?

thanks

Upvotes: 0

Views: 249

Answers (1)

Qaz
Qaz

Reputation: 61950

The documentation you brought up is a constructor. It's called when the object is actually made, not later. You're acting like the object is a functor, "calling" the object after it's made.

To utilize the constructor, you can initialize your member with the file name:

MyClass::MyClass() : fileMBox ("myFile.txt") {}

However, not having used Qt, I don't know if that will open it or not. If it does open it, use the below instead:

MyClass::MyClass() {
    fileMBox.setFileName ("myFile.txt");
}

Now your function just needs to open it, use it, and close it each time:

void MyClass::myMember() {
    if (!fileMBox.open (QIODevice::ReadOnly | QIODevice::Text))
        //handle error

    //read file and do whatever

    fileMBox.close(); 
}

Upvotes: 1

Related Questions