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;
}









share|improve this question




















  • 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















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;
}









share|improve this question




















  • 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













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;
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:54









Cœur

17.1k9102140




17.1k9102140










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














  • 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












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Three thoughts:




  1. fn::operator()(int, int) returns a bool but has no return statement. That function is not proper C++.


  2. 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.


  3. 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!





share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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:




  1. fn::operator()(int, int) returns a bool but has no return statement. That function is not proper C++.


  2. 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.


  3. 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!





share|improve this answer























  • 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

















up vote
2
down vote













Three thoughts:




  1. fn::operator()(int, int) returns a bool but has no return statement. That function is not proper C++.


  2. 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.


  3. 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!





share|improve this answer























  • 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















up vote
2
down vote










up vote
2
down vote









Three thoughts:




  1. fn::operator()(int, int) returns a bool but has no return statement. That function is not proper C++.


  2. 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.


  3. 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!





share|improve this answer














Three thoughts:




  1. fn::operator()(int, int) returns a bool but has no return statement. That function is not proper C++.


  2. 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.


  3. 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!






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python