You are not logged in.

Dear visitor, welcome to KDE-Forum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

1

Wednesday, May 26th 2004, 12:56pm

return QFile::errorString()

I have this function that opens a file and then does stuff. The function returns FALSE if the file couldn't be opened and TRUE if it could. Now I would like it to notify the error it encountered when it cannot open the file.

I thought that this was the correct way to do it:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool MyClass::DoSomethingWithAFile( const QString& path, QString* error )
{
   QFile file( path );
   if( file.open( IO_ReadOnly ) )
   {
      ...doing stuff
      file.close();
      return( TRUE );
   }
   else
   {
      *error = file.errorString();
      return( FALSE );
   }


and then call it like this:

Source code

1
2
3
4
5
6
7
some function:
   QString error;
   if( !DoSomethingWithAFile( "/path/to/file", &error )
   {
      ...stuff
      ...output error
   }


Unfortunately, it crashes in the line:
*error = file.errorString();
(if I comment it out the crash disappears)

I don't understand, because I thought that my line meant: "assign the QString returned by file.errorString() to the value pointed by error".

Can anybody tell me what the cause is of the crash, and equally important, how to solve it?

Thanks in advance.

wysota

Trainee

Posts: 65

Location: Warsaw, POLAND

  • Send private message

2

Wednesday, May 26th 2004, 1:49pm

RE: return QFile::errorString()

QString* is a pointer to a QString and not a QString itself. In the function you do *error = something() which means: "take the address error, look what it points at and assign the object that error points to the value of something()". You probably don't have an object "under" the pointer, try to replace the QString* error by QString& error and then assign error=file.errorString().
Live and let live - use the search engine.
"Use the docs Luke, use the docs!"

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

3

Wednesday, May 26th 2004, 3:34pm

Thanks for the answer wysota. I'm probably too hard-headed, but I still don't understand.

You say that it means: "take the address error, look what it points at..."

Isn't it pointing at a QString object? In the calling function I declared a QString error, and the I passed &error, which, as far as I know means "the address of the QString object error"


"...and assign the object that error points to the value of something()"

In my case, something() returns a QString and as I thought that error points to a QString, I thought it was valid to assign what something() returns to *error.

So I still don't understand.


As for your solution, I actually tried that, but a new problem arises. In the declaration of the DoSomethingWithAFile() function, I want to give a default value to the error parameter, because I'm not always interested in getting the error. When I use a pointer, I simply do:

bool DoSomethingWithAFile( const QString& path, QString* error = NULL );

but when I change the pointer to a reference, the compiler doesn't let me assign NULL as a default value:
error: invalid type `int' for default argument to `QString& '

I also tried to declare the default value QString::null, but that also gives me an error:
error: invalid type `const QString' for default argument to `QString&'

Finally, if I try "" (empty string) as a default
error: invalid type `const char[1]' for default argument to `QString&'

So how do I set a default value when I use QString& error?

wysota

Trainee

Posts: 65

Location: Warsaw, POLAND

  • Send private message

4

Wednesday, May 26th 2004, 4:02pm

I haven't noticed you call, sorry :) But still passing &error as QString* is not "cplusplusish", using references is better. If you don't want the error, then overload your function to take only the first argument and that's it... Did going through the reference instead of the pointer help? If not, read on...

What kind of a crash is it? Can you show the trace of it (using gdb)? Also instead of *error = file.errorString() try qDebug(file.errorString()) and see if it still dies (to see if the problem is in the assignment or the errorString() method).
Live and let live - use the search engine.
"Use the docs Luke, use the docs!"

anda_skoa

Professional

Posts: 1,273

Location: Graz, Austria

Occupation: Software Developer

  • Send private message

5

Wednesday, May 26th 2004, 4:11pm

Quoted

Originally posted by boyadg
In my case, something() returns a QString and as I thought that error points to a QString, I thought it was valid to assign what something() returns to *error.


It is valid. Perhaps you have given the second parameter a default value in the method's declaration (for example NULL or 0) and you forgot to check that before the assigment, thus dereferenceing NULL or 0

Cheers,
_
Qt/KDE Developer
Debian User

boyadg

Beginner

  • "boyadg" started this thread

Posts: 11

Location: Barcelona

  • Send private message

6

Wednesday, May 26th 2004, 8:12pm

Thanks both wysota and anda_skoa, your last answers cleared everything up.

wysota: Overloading is of course a possibility if I want to make the error argument optional, I hadn't thought of that. That works perfectly.

And anda_skoa: You found my mistake. Changing the line to
if( error != NULL ) *error = file.errorString();
solved the problem.

Thanks all, I'm learning a lot from you guys!