cannot convert bool to int

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

cannot convert bool to int

 
0
  #1
Jul 26th, 2008
hi,

i have the following code:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class test{
  5.  
  6. public:: bool t()
  7. {
  8. cout<<"enter your answer(y or n)\n";
  9.  
  10. int answer = 0;
  11. cin>>answer;
  12.  
  13. if (answer =='y') return true;
  14. else return false;
  15. }
  16.  
  17. };
  18.  
  19. int main(void){
  20.  
  21. test t;
  22. return t.t;
  23.  
  24. }

i get this error: cannot convert bool to int......... can anyone help me i am a newbie, the concept is fine i think as i am creating object of the class and calling the method in that class.

thanks
Last edited by Tekmaven; Jul 26th, 2008 at 2:14 pm. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: cannot convert bool to int

 
0
  #2
Jul 26th, 2008
You need to go back to basics

but the below seems to work:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class test
  5. {
  6.  
  7. public:
  8. bool t()
  9. {
  10. cout << "enter your answer(y or n)\n";
  11.  
  12. char answer;
  13. cin >> answer;
  14.  
  15. if ( answer == 'y' ) return true;
  16. else return false;
  17. }
  18.  
  19. };
  20.  
  21. int main ( void )
  22. {
  23.  
  24. test foo;
  25. cout << foo.t();
  26. cin.get();
  27. cin.get();
  28.  
  29. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: cannot convert bool to int

 
0
  #3
Jul 26th, 2008
thanks for quick reply, as i said i am a newbie

now i get the following error when compiling:

gcc class.cpp
/tmp/cc1LNGYB.o: In function `__static_initialization_and_destruction_0(int, int)':
class.cpp.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc1LNGYB.o: In function `__tcf_0':
class.cpp.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc1LNGYB.o: In function `main':
class.cpp.text+0x98): undefined reference to `std::cout'
class.cpp.text+0x9d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(bool)'
class.cpp.text+0xa4): undefined reference to `std::cin'
class.cpp.text+0xa9): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
class.cpp.text+0xb0): undefined reference to `std::cin'
class.cpp.text+0xb5): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
/tmp/cc1LNGYB.o: In function `test::t()':
class.cpp.text._ZN4test1tEv[test::t()]+0x11): undefined reference to `std::cout'
class.cpp.text._ZN4test1tEv[test::t()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
class.cpp.text._ZN4test1tEv[test::t()]+0x24): undefined reference to `std::cin'
class.cpp.text._ZN4test1tEv[test::t()]+0x29): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
/tmp/cc1LNGYB.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: cannot convert bool to int

 
0
  #4
Jul 26th, 2008
possibly #include <iostream> or using namespace std forgotten.

That also works: return answer == 'y' ;

instead of: if ( answer == 'y' ) return true; else return false;


krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: cannot convert bool to int

 
0
  #5
Jul 26th, 2008
You'd think someone with over 200 posts would have figured out either to use code tags, or turn off smilies.
At least enough to review the post before pressing submit.

What an unreadable mess!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: cannot convert bool to int

 
0
  #6
Jul 26th, 2008
>thanks for quick reply, as i said i am a newbie
>now i get the following error when compiling:

Post your entire code
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: cannot convert bool to int

 
0
  #7
Jul 26th, 2008
this is the entire code i have and i compile it under ubuntu gutsy using gcc class.cpp
  1. #include <iostream.h>
  2. using namespace std;
  3.  
  4. class test
  5. {
  6.  
  7. public:
  8. bool t()
  9. {
  10. cout << "enter your answer(y or n)\n";
  11.  
  12. char answer;
  13. cin >> answer;
  14.  
  15. if ( answer == 'y' ) return true;
  16. else return false;
  17. }
  18.  
  19. };
  20.  
  21. int main ( void )
  22. {
  23.  
  24. test foo;
  25. cout << foo.t();
  26. cin.get();
  27. cin.get();
  28.  
  29. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: cannot convert bool to int

 
0
  #8
Jul 26th, 2008
^^ i just got it right ....

i searched google and seems if you compile using g++ you wont get those header errors.

can anyone please explain to me the difference between g++ and gcc?

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: cannot convert bool to int

 
0
  #9
Jul 26th, 2008
I always thought gcc was to compile c code, but don't quote me on that. Also in the code you posted the iostream.h shouldn't have the .h in it.
Last edited by iamthwee; Jul 26th, 2008 at 1:51 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

Re: cannot convert bool to int

 
0
  #10
Jul 27th, 2008
What confuses me is that you are doing a cout on a function that you need to interact with... you should split the two into two seperate functions within the class, one being a setBool, the other being a getBool
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC