0

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

6
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by somemnguy
0

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();

}
0

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

0

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

0

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!

0

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

Post your entire code

0

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();

}
0

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

0

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.

0

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

0

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()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.