Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;" (http://www.velocityreviews.com/forums/t277450-compile-error-with-cout-hexadecimal-0x-hex-m-endl.html)

Ensoul Chee 09-08-2003 12:38 PM

compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"
 
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

Victor Bazarov 09-08-2003 02:00 PM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"
 
"Ensoul Chee" <mpub@sohu.com> 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



Sumit Rajan 09-08-2003 02:31 PM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"
 
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.


Frank Schmitt 09-08-2003 02:42 PM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"
 
mpub@sohu.com (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

Ron Natalie 09-08-2003 03:37 PM

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

"Frank Schmitt" <frank.schmitt@4sc.com> wrote in message news:4cllsz8hpj.fsf@scxw21.4sc...
> mpub@sohu.com (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.



Kevin Goodsell 09-08-2003 08:49 PM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m <<endl;"
 
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.


llewelly 09-08-2003 09:32 PM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m <<endl;"
 
Kevin Goodsell <usenet1.spamfree.fusion@neverbox.com> 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.

Ensoul Chee 09-09-2003 02:12 AM

Re: compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"
 
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.


All times are GMT. The time now is 01:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.