943,900 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5453
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 26th, 2008
0

cannot convert bool to int

Expand Post »
hi,

i have the following code:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Jul 26th, 2008
0

Re: cannot convert bool to int

You need to go back to basics

but the below seems to work:
C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 26th, 2008
0

Re: cannot convert bool to int

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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Jul 26th, 2008
0

Re: cannot convert bool to int

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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jul 26th, 2008
0

Re: cannot convert bool to int

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!
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 26th, 2008
0

Re: cannot convert bool to int

>thanks for quick reply, as i said i am a newbie
>now i get the following error when compiling:

Post your entire code
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 26th, 2008
0

Re: cannot convert bool to int

this is the entire code i have and i compile it under ubuntu gutsy using gcc class.cpp
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Jul 26th, 2008
0

Re: cannot convert bool to int

^^ 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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Jul 26th, 2008
0

Re: cannot convert bool to int

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 27th, 2008
0

Re: cannot convert bool to int

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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC