Hi, everyone. I am studying Qt4. I want to write a function that reads from a file and the write to the same file. It is supposed to be simple, but I can't make it through the compiler.

Here is my header file:

#include <QFile>
#include <QString>
#include <QStringList>
#include <QTextStream>

class rwFile
{ 
	private:
		static QFile sm_fileData;
	public:
		rwFile(QString fileName);
		QStringList readFile();
		void writeFile(QStringList LstData);
};

Here is my cpp file:

rwFile::rwFile(QString fileName)
{
	sm_fileData(fileName); //  ***Error here!!
}


QStringList rwFile::readFile()
{
	QStringList LstLines;
	sm_fileData.open(QIODevice::ReadOnly);
	QTextStream readData(&sm_fileData);
	while (!readData.atEnd())
	{
		LstLines << readData.readLine();
	}

	sm_fileData.close();
	return LstLines;
}

void rwFile::writeFile(QStringList LstData)
{
	sm_fileData.open(QIODevice::WriteOnly);
	QTextStream writeData(&sm_fileData);
	foreach(QString data, LstData)
	{
		writeData << data << endl;
	}
	sm_fileData.close();
}

The error message (in the line marked by *** in my cpp file) is "no match for call to ‘(QFile) (QString&)’".
Could anybody help me with this class? Thanks

Recommended Answers

All 2 Replies

Since sm_fileData is static member, wherever you are defining it, at that point the default ctor of class QFile is getting called and sm_fileData is getting initialized. You cannot call another ctor on it at any later point of time. You can try using the QFile's open function (cross check the documentation for this, i'm not 100% sure of the available fns) to attach it to the user given file name, if you know the file name at compile time, just initialize it with it at the first instance.

Thank you, guys. I got it fixed by using pointer.

It should be :

pm_fileData = new QFile( fileName );

where: QFile* pm_fileData;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.