function doesn't return simply skip the return line

H

hirsh.dan

Hello to all, i have the following functions:

string File::readLine(){
char ch;
string str;
ch = read();
while(ch != LF && ch != CR && ch != -1){
str.append(1,ch);
ch = read();
}
if(ch == -1){
cout << "this line could not be read" << endl;
exit(1);
}
//take care of situation where the loop exited with LF but
//on a system that has CR.
int tmp = file->tellg();
if(read() != CR){ //if no CR available on system, return the get
pointer to it's correct location.
file->seekg(tmp);
}
return str;
}

the function is executed correctly but in the end it skips the "return
str" line,
and tun flies with an error.
debugging took me to a qt class moc_myclass.cpp (it's a qt project in
eclipse)
to a line where it says "_id -= 1;"
inside a function called "int myclass::qt_metacall(...)"

can anyone help me??????????
 
H

hirsh.dan

Since 'qt' is not part of C++, you should consider asking in the
newsgroup (or a web forum) dedicated to that. Apparently the code you
posted cannot be verified without the rest of the stuff (which you did
not post).

V

maybe, but it skips a regular c++ line of code,
also if i put a cout before the call to return, it prints out (goes
there) but just before
return is supposed to execute, it flies
don't you have any ideas why might that be happening,
isn't my c++ code written well, what else should i post for you to get
the whole picture
?
 
H

hirsh.dan

How do you know? Did you step through it in the debugger? If the error
is returned, it's possible that an exception is thrown and then caught
by some invisible code. Qt runs some files through their own "meta
compiler" or something, and who know what kind of code they insert into
what you think is "regular c++"...


Examine the assembly.


Nope. Qt is not part of C++, and the sheer fact that you are using it
can render your program incomprehensible from the "regular c++" POV.


Again, the code you posted is fine. However, you didn't post enough of
it, for example, what's happening in the 'read()' function?

V

well, thank you Victor,
in my next post i shell reference your guidance on writing a thread
that
is more understood, and acceptable.
have a good day.

P.S
i guess the capital A was for a new line, but i wasn't innuendoing
anything
next time I will write only letters.
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
[snip]
the function is executed correctly but in the end it skips the "return
str" line,
and tun flies with an error.
debugging took me to a qt class moc_myclass.cpp (it's a qt project in
eclipse)
to a line where it says "_id -= 1;"
inside a function called "int myclass::qt_metacall(...)"

can anyone help me??????????

From Trolltech documentation:
http://doc.trolltech.com/qq/qq16-dynamicqobject.html
<<<
When a signal is emitted, Qt uses qt_metacall() to invoke the slots
connected to the signal. The first parameter, call, is then set to
QMetaObject::InvokeMetaMethod. (The qt_metacall() function is also used
for other types of access to the meta-object, such as setting or getting
properties.)
 
J

James Kanze

Hello to all, i have the following functions:
string File::readLine(){
char ch;
string str;
ch = read();
while(ch != LF && ch != CR && ch != -1){

Nothing to do with your problem, but there's no guarantee that a
char can ever take on the value of -1, so you've definitely got
something wrong here. I don't know a read() function which
takes no arguments, so I can't be sure, but the usual idiom
(from C) for this sort of thing is for functions which read a
single character to return an int, with a value in the range
0...UCHAR_MAX, or a negative value for EOF. But this supposes
that the returned value is stored in an int, and not a char.
str.append(1,ch);

Rather than "str += ch"? Strange.
ch = read();
}
if(ch == -1){
cout << "this line could not be read" << endl;
exit(1);
}
//take care of situation where the loop exited with LF but
//on a system that has CR.
int tmp = file->tellg();

Do you know what type ifstream::tellg returns? It isn't int.
if(read() != CR){ //if no CR available on system, return the get
pointer to it's correct location.
file->seekg(tmp);
}

What's the relationship between file and where ever read()
accesses. If read() is accessing file, and file is an istream,
why not use file directly, e.g.:

if ( file->peek() == CR ) {
file->get() ;
}
return str;
}
the function is executed correctly but in the end it skips the "return
str" line,
and tun flies with an error.

What does that mean? "Tun flies with an error." I don't
understand it. If you reach the return statement, it will be
executed. It may cause an exception to be thrown (because str
must be copied, and copying a string may throw), but unless
you're right at the limit in memory use, it's highly unlikely.

Another possibility (very likely, IMHO) is that you've corrupted
the free space arena sometime earlier, and you just happened to
hit the problem when copying str.
debugging took me to a qt class moc_myclass.cpp (it's a qt
project in eclipse)
to a line where it says "_id -= 1;"
inside a function called "int myclass::qt_metacall(...)"

I doubt that that's significant, but you never know. More
likely something else is corrupt, which results in your stack
being corrupted, which results in your returning someplace else
than where you came from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top