954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cannot convert bool to int

hi,

i have the following code:

#include <iostream>
using namespace std;

class test{

public:: bool t()
{
cout<<"enter your answer(y or n)\n";

int answer = 0;
cin>>answer;

if (answer =='y') return true;
else return false;
}

};

int main(void){

test t;
return t.t;

}


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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

You need to go back to basics

but the below seems to work:

#include <iostream>
using namespace std;

class test
{

  public:
  bool t()
  {
    cout << "enter your answer(y or n)\n";

    char answer;
    cin >> answer;

    if ( answer == 'y' ) return true;
    else return false;
  }

};

int main ( void )
{

  test foo;
  cout << foo.t();
  cin.get();
  cin.get();

}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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 >::operator<<(bool)'
class.cpp:(.text+0xa4): undefined reference to `std::cin'
class.cpp:(.text+0xa9): undefined reference to `std::basic_istream >::get()'
class.cpp:(.text+0xb0): undefined reference to `std::cin'
class.cpp:(.text+0xb5): undefined reference to `std::basic_istream >::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 >& std::operator<< >(std::basic_ostream >&, 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 >& std::operator>> >(std::basic_istream >&, char&)'
/tmp/cc1LNGYB.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

possibly #include or using namespace std forgotten.

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

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


krs,
tesu

tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 158
Solved Threads: 98
 

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!

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

Post your entire code

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

this is the entire code i have and i compile it under ubuntu gutsy using gcc class.cpp

#include <iostream.h>
using namespace std;

class test
{

  public:
  bool t()
  {
    cout << "enter your answer(y or n)\n";

    char answer;
    cin >> answer;

    if ( answer == 'y' ) return true;
    else return false;
  }

};

int main ( void )
{

  test foo;
  cout << foo.t();
  cin.get();
  cin.get();

}
sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

^^ 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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

Manutebecker
Junior Poster in Training
51 posts since May 2008
Reputation Points: 10
Solved Threads: 3
 

hi,

you may read this

http://gcc.gnu.org/

or that

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/G_002b_002b-and-GCC.html

-----
tesu


return answer == 'y' ;

tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 158
Solved Threads: 98
 

Hmm it seems like when you're returning t.t, it's going to give you a bool value from that, but the main function that's supposed to be returning it is an int type...ie int main()

somemnguy
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You