function object with stdout in C++
up vote
1
down vote
favorite
the program will run correctly if with no cout;
why?
something wrong with output cache?
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
class fn
{
public:
int i;
bool operator()(int,int)
{
++i;
cout<<"what the poodles?n";
}
};
int main()
{
vector<int> temp(9,9);
vector<int> tmp(2,3);
fn f;
vector<int>::iterator ite;
ite=find_first_of(temp.begin(),temp.end(),tmp.begin(),tmp.end(),f);
if(ite==temp.end())cout<<"Pomeranians!n";
//cout<<"compared "<<f.i<<" time(s)n";//if note this ,you'll get defferent output.
return 0;
}
c++ function-object
add a comment |
up vote
1
down vote
favorite
the program will run correctly if with no cout;
why?
something wrong with output cache?
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
class fn
{
public:
int i;
bool operator()(int,int)
{
++i;
cout<<"what the poodles?n";
}
};
int main()
{
vector<int> temp(9,9);
vector<int> tmp(2,3);
fn f;
vector<int>::iterator ite;
ite=find_first_of(temp.begin(),temp.end(),tmp.begin(),tmp.end(),f);
if(ite==temp.end())cout<<"Pomeranians!n";
//cout<<"compared "<<f.i<<" time(s)n";//if note this ,you'll get defferent output.
return 0;
}
c++ function-object
2
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
2
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
4
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
the program will run correctly if with no cout;
why?
something wrong with output cache?
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
class fn
{
public:
int i;
bool operator()(int,int)
{
++i;
cout<<"what the poodles?n";
}
};
int main()
{
vector<int> temp(9,9);
vector<int> tmp(2,3);
fn f;
vector<int>::iterator ite;
ite=find_first_of(temp.begin(),temp.end(),tmp.begin(),tmp.end(),f);
if(ite==temp.end())cout<<"Pomeranians!n";
//cout<<"compared "<<f.i<<" time(s)n";//if note this ,you'll get defferent output.
return 0;
}
c++ function-object
the program will run correctly if with no cout;
why?
something wrong with output cache?
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
class fn
{
public:
int i;
bool operator()(int,int)
{
++i;
cout<<"what the poodles?n";
}
};
int main()
{
vector<int> temp(9,9);
vector<int> tmp(2,3);
fn f;
vector<int>::iterator ite;
ite=find_first_of(temp.begin(),temp.end(),tmp.begin(),tmp.end(),f);
if(ite==temp.end())cout<<"Pomeranians!n";
//cout<<"compared "<<f.i<<" time(s)n";//if note this ,you'll get defferent output.
return 0;
}
c++ function-object
c++ function-object
edited Nov 11 at 13:54
Cœur
17.2k9102140
17.2k9102140
asked Mar 14 '13 at 16:43
tqtifnypmb
63
63
2
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
2
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
4
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53
add a comment |
2
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
2
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
4
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53
2
2
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
2
2
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
4
4
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
Three thoughts:
fn::operator()(int, int)
returns abool
but has no return statement. That function is not proper C++.When I fix that line, I get the output that I would expect. If the first part of the answer doesn't fix your problem, could you elaborate in your question with the output that you see, and the output that you expect, and a note about how they differ.
You're also incrementing an uninitialized variable
fn::i
. This isn't going to do anything helpful for you. You should initialize it in a constructor. If you attempt to print this variable (or inspect it in any way), it could have any value, because it's starting value could have been anything (possibly 0, possibly anything else).
To elaborate, my compiler warned me of the following problem:
foo.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
To fix this, I added a return false;
at the end of the functor, and I see the following output, which makes sense to me.
[11:47am][wlynch@watermelon /tmp] ./foo
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
Pomeranians!
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Three thoughts:
fn::operator()(int, int)
returns abool
but has no return statement. That function is not proper C++.When I fix that line, I get the output that I would expect. If the first part of the answer doesn't fix your problem, could you elaborate in your question with the output that you see, and the output that you expect, and a note about how they differ.
You're also incrementing an uninitialized variable
fn::i
. This isn't going to do anything helpful for you. You should initialize it in a constructor. If you attempt to print this variable (or inspect it in any way), it could have any value, because it's starting value could have been anything (possibly 0, possibly anything else).
To elaborate, my compiler warned me of the following problem:
foo.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
To fix this, I added a return false;
at the end of the functor, and I see the following output, which makes sense to me.
[11:47am][wlynch@watermelon /tmp] ./foo
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
Pomeranians!
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
add a comment |
up vote
2
down vote
Three thoughts:
fn::operator()(int, int)
returns abool
but has no return statement. That function is not proper C++.When I fix that line, I get the output that I would expect. If the first part of the answer doesn't fix your problem, could you elaborate in your question with the output that you see, and the output that you expect, and a note about how they differ.
You're also incrementing an uninitialized variable
fn::i
. This isn't going to do anything helpful for you. You should initialize it in a constructor. If you attempt to print this variable (or inspect it in any way), it could have any value, because it's starting value could have been anything (possibly 0, possibly anything else).
To elaborate, my compiler warned me of the following problem:
foo.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
To fix this, I added a return false;
at the end of the functor, and I see the following output, which makes sense to me.
[11:47am][wlynch@watermelon /tmp] ./foo
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
Pomeranians!
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
add a comment |
up vote
2
down vote
up vote
2
down vote
Three thoughts:
fn::operator()(int, int)
returns abool
but has no return statement. That function is not proper C++.When I fix that line, I get the output that I would expect. If the first part of the answer doesn't fix your problem, could you elaborate in your question with the output that you see, and the output that you expect, and a note about how they differ.
You're also incrementing an uninitialized variable
fn::i
. This isn't going to do anything helpful for you. You should initialize it in a constructor. If you attempt to print this variable (or inspect it in any way), it could have any value, because it's starting value could have been anything (possibly 0, possibly anything else).
To elaborate, my compiler warned me of the following problem:
foo.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
To fix this, I added a return false;
at the end of the functor, and I see the following output, which makes sense to me.
[11:47am][wlynch@watermelon /tmp] ./foo
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
Pomeranians!
Three thoughts:
fn::operator()(int, int)
returns abool
but has no return statement. That function is not proper C++.When I fix that line, I get the output that I would expect. If the first part of the answer doesn't fix your problem, could you elaborate in your question with the output that you see, and the output that you expect, and a note about how they differ.
You're also incrementing an uninitialized variable
fn::i
. This isn't going to do anything helpful for you. You should initialize it in a constructor. If you attempt to print this variable (or inspect it in any way), it could have any value, because it's starting value could have been anything (possibly 0, possibly anything else).
To elaborate, my compiler warned me of the following problem:
foo.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
To fix this, I added a return false;
at the end of the functor, and I see the following output, which makes sense to me.
[11:47am][wlynch@watermelon /tmp] ./foo
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
what the poodles?
Pomeranians!
edited Mar 14 '13 at 16:55
answered Mar 14 '13 at 16:50
Bill Lynch
59.4k1194142
59.4k1194142
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
add a comment |
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
thx~~ I added a return false;and i got the same output ;
– tqtifnypmb
Mar 15 '13 at 5:20
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
but why it also run right if I note the statement with cout,before I added a return false?(the output same as you posted).....ps variable i just to count,I used global variable in my code;
– tqtifnypmb
Mar 15 '13 at 5:47
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
The code you had written before you added the return false was not valid code. You must end a function with a return statement, or the entire program is invalid, and weird side-effects can occur. These side-effects may be what you expect, or not what you expect. Also, about your cout statement, please read my third point, and let me know if that doesn't make sense.
– Bill Lynch
Mar 15 '13 at 14:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15415236%2ffunction-object-with-stdout-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Any reason for the profanity in the code?
– Silas
Mar 14 '13 at 16:44
2
You have a functor that returns a bool, that has not return statement.
– Bill Lynch
Mar 14 '13 at 16:46
4
Fixed it in style :p
– John Humphreys - w00te
Mar 14 '13 at 16:46
@w00te Brilliant!
– Alex Chamberlain
Mar 14 '13 at 16:46
You should consider compiler warnings to be as bad as errors :) They cause far more frustration than errors do.
– John Humphreys - w00te
Mar 14 '13 at 16:53