Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"

Reply
Thread Tools

compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"

 
 
Ensoul Chee
Guest
Posts: n/a
 
      09-08-2003
I used
#include <iostream.h>
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

to print value of m in hexadecimal mode.

But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?

OS: Redhat 9
Compiler: gcc 3.2.2
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2003
"Ensoul Chee" <> wrote...
> I used
> #include <iostream.h>


Stop using this non-standard header. Its time have long been over.
Start using <iostream>. Read about 'std' namespace as well.

> int m;
> cout << "Hexadecimal == 0x" << hex << m << endl;


'm' is used here without being initialised. That is a very bad
mistake on some systems.

>
> to print value of m in hexadecimal mode.


'm' has indeterminate value. You can't really expect to see anything
sensible.

>
> But I got the compile error like this
>
> couttest.cpp:20 `hex' undeclared (first use this function)
>
> What's wrong with the code?


Use of non-standard header. The code is a non-compilable fragment.
Perhaps you should read FAQ 5.8.

> OS: Redhat 9
> Compiler: gcc 3.2.2


#include <iostream>
int main()
{
int m = 42;
std::cout << "Hex of " << m << " is " \
<< std::hex << m << std::endl;
}

Victor


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      09-08-2003
Ensoul Chee wrote:

> I used
> #include <iostream.h>
> int m;
> cout << "Hexadecimal == 0x" << hex << m << endl;
>
> to print value of m in hexadecimal mode.
>
> But I got the compile error like this
>
> couttest.cpp:20 `hex' undeclared (first use this function)
>
> What's wrong with the code?



The problem is that hex resides in the std namespace. So you will need to
try something like:


#include <iostream>

int main()
{
int m = 255;
std::cout << "Hexadecimal == 0x" << std::hex << m << '\n';
}

HTH,
Sumit.

 
Reply With Quote
 
Frank Schmitt
Guest
Posts: n/a
 
      09-08-2003
(Ensoul Chee) writes:

> I used
> #include <iostream.h>


This header is deprecated, use #include <iostream>

> int m;
> cout << "Hexadecimal == 0x" << hex << m << endl;
>
> to print value of m in hexadecimal mode.
>
> But I got the compile error like this
>
> couttest.cpp:20 `hex' undeclared (first use this function)
>
> What's wrong with the code?


Everything from the standard library lives in the std:: namespace,
so you have to use the prefix std:: or an using declaration/directive.
The following example should work:

#include <iostream>

int main() {
int m = 13;
std::cout << "Hexadecimal == 0x" << std::hex << m << std::endl;
return 0;
}

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-08-2003

"Frank Schmitt" <> wrote in message news:4cllsz8hpj.fsf@scxw21.4sc...
> (Ensoul Chee) writes:
>
> > I used
> > #include <iostream.h>

>
> This header is deprecated, use #include <iostream>


It's not deprecated, it's just plain wrong. iostream.h is not
part of the standard language.


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-08-2003
Ensoul Chee wrote:

> I used
> #include <iostream.h>
> int m;
> cout << "Hexadecimal == 0x" << hex << m << endl;
>
> to print value of m in hexadecimal mode.
>
> But I got the compile error like this
>
> couttest.cpp:20 `hex' undeclared (first use this function)
>
> What's wrong with the code?
>
> OS: Redhat 9
> Compiler: gcc 3.2.2


In addition to the other replies, std::hex is technically presented in
<ios>. <ios> is probably #included in <iostream>, but I don't know if
this is required.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      09-08-2003
Kevin Goodsell <> writes:

> Ensoul Chee wrote:
>
>> I used
>> #include <iostream.h>
>> int m;
>> cout << "Hexadecimal == 0x" << hex << m << endl;
>> to print value of m in hexadecimal mode. But I got the compile error
>> like this
>> couttest.cpp:20 `hex' undeclared (first use this function)
>> What's wrong with the code?
>> OS: Redhat 9
>> Compiler: gcc 3.2.2

>
> In addition to the other replies, std::hex is technically presented in
> <ios>. <ios> is probably #included in <iostream>, but I don't know if
> this is required.


It's not. <iostream> requires several things from <ios>, but AFAIK,
it doesn't require the manipulators. So you can't rely on them
being provided by <iostream> . Really, all <iostream> provides is
cin, clog, cerr, cout, and their wide character equivalents. Lots
of other stuff comes long just becuase those objects have complex
types, which require many things, but not the manipulators.
 
Reply With Quote
 
Ensoul Chee
Guest
Posts: n/a
 
      09-09-2003
Thanks for your reply.

I think I need to find a good book to learn c++.

That code is copy from a book about GNU C++ for linux program.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
hex string to hex value tim Python 8 11-23-2005 06:27 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57