Connecting Tech Pros Worldwide Help | Site Map

function doesn't return simply skip the return line

  #1  
Old July 21st, 2008, 03:55 PM
hirsh.dan@gmail.com
Guest
 
Posts: n/a
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??????????
  #2  
Old July 21st, 2008, 04:05 PM
Victor Bazarov
Guest
 
Posts: n/a

re: function doesn't return simply skip the return line


hirsh.dan@gmail.com wrote:
Quote:
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??????????
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #3  
Old July 21st, 2008, 07:45 PM
hirsh.dan@gmail.com
Guest
 
Posts: n/a

re: function doesn't return simply skip the return line


On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
hirsh....@gmail.com wrote:
Quote:
Hello to all, i have the following functions:
>
Quote:
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;
}
>
Quote:
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(...)"
>
Quote:
can anyone help me??????????
>
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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
?
  #4  
Old July 21st, 2008, 07:55 PM
Victor Bazarov
Guest
 
Posts: n/a

re: function doesn't return simply skip the return line


hirsh.dan@gmail.com wrote:
Quote:
On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
>hirsh....@gmail.com wrote:
Quote:
>>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??????????
>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
>--
>Please remove capital 'A's when replying by e-mail
>I do not respond to top-posted replies, please don't ask
>
maybe, but it skips a regular c++ line of code,
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++"...
Quote:
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
Examine the assembly.
Quote:
don't you have any ideas why might that be happening,
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.
Quote:
isn't my c++ code written well, what else should i post for you to get
the whole picture
?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #5  
Old July 22nd, 2008, 05:55 AM
hirsh.dan@gmail.com
Guest
 
Posts: n/a

re: function doesn't return simply skip the return line


On 21 יולי, 21:49, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
hirsh....@gmail.com wrote:
Quote:
On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
hirsh....@gmail.com wrote:
>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??????????
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).
>
Quote:
Quote:
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>
Quote:
maybe, but it skips a regular c++ line of code,
>
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++"...
>
Quote:
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
>
Examine the assembly.
>
Quote:
don't you have any ideas why might that be happening,
>
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.
>
Quote:
isn't my c++ code written well, what else should i post for you to get
the whole picture
?
>
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.
  #6  
Old July 22nd, 2008, 11:05 AM
James Kanze
Guest
 
Posts: n/a

re: function doesn't return simply skip the return line


On Jul 21, 4:50 pm, hirsh....@gmail.com wrote:
Quote:
Hello to all, i have the following functions:
Quote:
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.
Quote:
str.append(1,ch);
Rather than "str += ch"? Strange.
Quote:
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.
Quote:
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() ;
}
Quote:
return str;
}
Quote:
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.
Quote:
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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem loading an image from client side code Adam Sandler answers 11 January 26th, 2007 09:15 PM
problem with providing an image src property Adam Sandler answers 0 January 23rd, 2007 09:15 PM
please comment hellfire answers 3 November 14th, 2005 12:47 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM