| | |
cannot convert bool to int
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
hi,
i have the following code:
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
i have the following code:
C++ Syntax (Toggle Plain Text)
#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
Last edited by Tekmaven; Jul 26th, 2008 at 2:14 pm. Reason: Code tags
You need to go back to basics
but the below seems to work:
but the below seems to work:
C++ Syntax (Toggle Plain Text)
#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(); }
*Voted best profile in the world*
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

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
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
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.
this is the entire code i have and i compile it under ubuntu gutsy using gcc class.cpp
C++ Syntax (Toggle Plain Text)
#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(); }
![]() |
Similar Threads
- Create buttons at runtime (C#)
- How to hook a MyCopyFileW function in OS (C++)
- how to convert true/false to bit (ASP.NET)
- Convert first character from lowercase to uppercase? (C)
- Logic to Convert Days From 1800 to a Date (Month, Day, Year) (C++)
- Convert first character from lowercase to uppercase? (C++)
- storing large numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: need help for a program (very urgent)
- Next Thread: compile vc6 project
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






